当对象键存在于javascript中另一个对象的数组中时,重组对象数据

我有一个对象数据,它具有“名称”,“日期”等属性

const data = {
    "name":{
      "columnName":"name","columnType":"name of employee","values":[
        "sam","son"
      ],"range":{
        "min":0,"max":0
      }
    },"date":{
      "columnName":"date","columnType":"date input","categoricalValues":[
      ],"fare":{
      "columnName":"fare","columnType":"fare indication","values":[
        "false","true"
      ],"id":{
      "columnName":"id","columnType":"id employee","values":[
      ],"max":0
      }
    }
}

另一个对象 categoricalColumns ,它具有一些属性,其中包含一个数组,在其中指定了先前对象的所有属性名称

const categoricalColumns = 
  { 
    "Charges" : ["name","fare"],"Location" : ["date","address" ]
  }

如果对象 data 的属性,例如 categoricalColumns 中包含“名称”和“ flare”,我需要在其中重新构造categoricalcolumn对象此格式

{
    "title" : "Charges","children" : [
      {
        "name":{
          "columnName":"name","values":[
            "sam","son"
          ],"range":{
            "min":0,"max":0
          }
        }
      },{
        "fare":{
          "columnName":"fare","values":[
            "false","true"
          ],"max":0
          }
        }
      }
    ]
  }

预期结果:

const result = [
  {
    "title" : "Charges","max":0
          }
        }
      }
    ]
  },{
    "title" : "Location","children" : [
      {
        "date":{
          "columnName":"date","categoricalValues":[
          ],{
    title : "Others","children" : [
      {
        "id":{
          "columnName":"id","values":[
          ],"max":0
          }
        }
      }
    ]
  }
]

如果 data 的某些属性与任何 categoricalColumns 不匹配,则必须在“ Others”属性的子级中。

ayihaimin 回答:当对象键存在于javascript中另一个对象的数组中时,重组对象数据

您可以使用Set并删除访问过的列。

function restructure(data,categories) {
    var others = new Set(Object.keys(data));

    return Object
        .entries(categories)
        .reduce((r,[title,category]) => {
            var children = category.filter(k => k in data).map(k => ({ [k]: data[k] }));
            if (children.length) r.push({ title,children });
            category.forEach(Set.prototype.delete,others);
            return r;
        },[])
        .concat(others.size
            ? { title: 'Others',children: Array.from(others,k => ({ [k]: data[k] })) }
            : []
        );
}


var data = { name: { columnName: "name",columnType: "name of employee",values: ["sam","son"],range: { min: 0,max: 0 } },date: { columnName: "date",columnType: "date input",categoricalValues: [],fare: { columnName: "fare",columnType: "fare indication",values: ["false","true"],id: { columnName: "id",columnType: "id employee",values: [],max: 0 } } },categoricalColumns = { Charges: ["name","fare"],Location: ["date","address"] },result = restructure(data,categoricalColumns);

   console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

,

const data = {
        "name": {
            "columnName": "name","columnType": "name of employee","values": [
                "sam","son"
            ],"range": {
                "min": 0,"max": 0
            }
        },"date": {
            "columnName": "date","columnType": "date input","categoricalValues": [
            ],"fare": {
            "columnName": "fare","columnType": "fare indication","values": [
                "false","true"
            ],"id": {
            "columnName": "id","columnType": "id employee","values": [
            ],"max": 0
            }
        }
    }
    const categoricalColumns = {
        "Charges": ["name","Location": ["date","address"]
    }

    let result_obj = {};
    
    // add data elements to result_obj according to categoricalColumns:
    for (let j in data) {
        for (let k in categoricalColumns) {
            if (categoricalColumns[k].indexOf(j) > -1) {
                if (typeof result_obj[k] == 'undefined') {
                    result_obj[k] = [];
                }
                let obj = {};
                obj[data[j].columnName] = data[j];
                result_obj[k].push(obj);
                delete data[j];
                break;
            }
        }
    }
    // add remaining elements to "Others" category:
    for (let j in data) {
        if (typeof result_obj['Others'] == 'undefined') {
            result_obj['Others'] = [];
        }
        let obj = {};
        obj[data[j].columnName] = data[j];
        result_obj['Others'].push(obj);
        delete data[j];
    }
    // create result array from result_obj
    let result = [];
    for (let j in result_obj) {
        result.push({
            "title": j,"children": result_obj[j]
        });
    }

    console.log(result);

,
let newCategorial = [{title: 'Others',children: []}];
let index;
Object.keys(data).forEach( dataKey => {
index = '';
  for(categorialKey in Object.keys(categorialColumns)) {
     index = categorialColumns[categorialKey].indexOf(dataKey);
     if(index != -1){
       newCatIndex = newCategorial.findIndex( obj => obj.title === categorialKey);
       newCatIndex === -1? newCategorial[categorialKey].push({ title: categorialKey,children: data[dataKey]) : newCategorial[categorialKey].children.push(data[dataKey]);
     }      
  }
  if(index == -1)
     newCategorial['Others'].push(data[dataKey]);
}
本文链接:https://www.f2er.com/3166397.html

大家都在问