通过另一个预定义的Array元素作为键来重组JavaScript对象数组

时间:2018-12-03 13:58:30

标签: javascript arrays object

我也在jsbin上放置了相同的代码:https://jsbin.com/literefeqo/edit?js,console

说明

我有一个对象数组(1),想转换(可能使用地图)此对象。转换条件是给定的数组(2),它对应于german中的arrObj属性。这意味着,如果german中有一个arrObj属性,则应“复制”该属性并将其用作生成resultObj(3)的键。如果没有german属性,则密钥应为“未知”或其他任何值。

注意:resultObj中可能有更多条目,例如Montag。这样resultObj.Montag[i]应该是对象数组。

(1)对象数组

const arrObj = [
{
    "day": {
    "string": "Monday",
    "Number": 1
    },
    "description": {
    "type": "string",
    "value": "The first day of the week"
    },
    "german": {
    "type": "string",
    "value": "Montag"
    }
},
{
    "day": {
    "string": "Tuesday",
    "Number": 2
    },
    "description": {
    "type": "string",
    "value": "The second day of the week"
    }
},
{
    "day": {
    "string": "Wednesday",
    "Number": 3
    },
    "description": {
    "type": "string",
    "value": "The third day of the week"
    },
    "german": {
    "type": "string",
    "value": "Mittwoch"
    }
}
];

(2)应该成为新对象键的数组

const germanDays = ["Montag","Dienstag","Mittwoch","Donnerstag"];

(3)结果应类似于

const resultObj =   {
"Montag": [
  {
    "day": {
      "string": "Monday",
      "Number": 1
    },
    "description": {
      "type": "string",
      "value": "The first day of the week"
    },
    "german": {
      "type": "string",
      "value": "Montag"
    }
  }
],
"Dienstag": [
  {}
],
"Mittwoch": [
  {
    "day": {
      "string": "Wednesday",
      "Number": 3
    },
    "description": {
      "type": "string",
      "value": "The third day of the week"
    },
    "german": {
      "type": "string",
      "value": "Mittwoch"
    }
  }
],
"Donnerstag": [
  {}
],
"Unknown": [
  {
    "day": {
      "string": "Tuesday",
      "Number": 2
    },
    "description": {
      "type": "string",
      "value": "The second day of the week"
    }
  }
]

};

2 个答案:

答案 0 :(得分:3)

  

(可能使用地图)

map,用于Array到Array的映射,一个更合适的功能是reduce。

这里是一个例子。

const germanDays = ["Montag","Dienstag","Mittwoch","Donnerstag"]

const arrObj = [
{
    "day": {
    "string": "Monday",
    "Number": 1
    },
    "description": {
    "type": "string",
    "value": "The first day of the week"
    },
    "german": {
    "type": "string",
    "value": "Montag"
    }
},
{
    "day": {
    "string": "Tuesday",
    "Number": 2
    },
    "description": {
    "type": "string",
    "value": "The second day of the week"
    }
},
{
    "day": {
    "string": "Wednesday",
    "Number": 3
    },
    "description": {
    "type": "string",
    "value": "The third day of the week"
    },
    "german": {
    "type": "string",
    "value": "Mittwoch"
    }
},
{
    "day": {
    "string": "Monday",
    "Number": 1
    },
    "description": {
    "type": "string",
    "value": "Just another text is here"
    },
    "german": {
    "type": "string",
    "value": "Montag"
    }
}
];

const ret = germanDays.reduce((a, v) => {
  const f = arrObj.filter(f => f.german && f.german.value === v);
  a[v] = f;
  return a;
}, {
  "Unknown": arrObj.filter(f => !f.german)
});


console.log(ret);

答案 1 :(得分:2)

类似这样的东西(单击“运行代码片段”按钮):

const arrObj = [
{
    "day": {
    "string": "Monday",
    "Number": 1
    },
    "description": {
    "type": "string",
    "value": "The first day of the week"
    },
    "german": {
    "type": "string",
    "value": "Montag"
    }
},
{
    "day": {
    "string": "Tuesday",
    "Number": 2
    },
    "description": {
    "type": "string",
    "value": "The second day of the week"
    }
},
{
    "day": {
    "string": "Wednesday",
    "Number": 3
    },
    "description": {
    "type": "string",
    "value": "The third day of the week"
    },
    "german": {
    "type": "string",
    "value": "Mittwoch"
    }
}
];


const germanDays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag"];

const resultObj = {}

for (const item of arrObj) {
    if (item.german && item.german.value && germanDays.includes(item.german.value)) {
        addVal(item.german.value, item)
    } else {
        addVal('unknown', item)
    }
}

// helper func
function addVal(key, val) {
    if(!resultObj[key]) {
        resultObj[key] = []
    }
    resultObj[key].push(val)
}

console.log(resultObj)