简体   繁体   English

我想创建一个 object,其中键作为 object 的属性,值作为与键匹配的对象数组(javascript)

[英]I want to create an object with the key as a property from the object and value as an array of objects matching the key (javascript)

I have a given object to iterate and create another object in the below format (newObj) Given object:我有一个给定的 object 以下面的格式(newObj)迭代并创建另一个 object 给定 object:

let obj = [
  {a: 1, b: 232},
  {a: 1, b: 2},
  {a: 1, b: 256},
  {a: 2, b: 3},
  {a: 2, b: 3343},
  {a: 3, b: 4}
];

Expected object:预计 object:

newObj = {
    1: [{a: 1, b: 232}, {a: 1, b: 2}, {a: 1, b: 256}],
    2: [{a: 2, b: 3}],
    3: [{a: 3, b: 4}]
}

Code:代码:

let newObj = {};

obj.forEach(element => {
  if (newObj[element.a]) {
    let key = element.a;
    newObj[key] = newObj[key].push(element);
  }
  newObj[element.a] = [element];
});

console.log(newObj);

We create a result object, loop through every object in obj array, if we don't have the object a key in the result object, we add him ( a: [] ), and after that we push the entire object to result[a] array我们创建一个result object,循环遍历obj数组中的每个 object,如果我们在结果 object 中没有 object a键,我们添加他( a: [] ),然后我们将整个 object push 到result[a]数组

 let obj = [ {a: 1, b: 232}, {a: 1, b: 2}, {a: 1, b: 256}, {a: 2, b: 3}, {a: 2, b: 3343}, {a: 3, b: 4} ]; let result = {}; for(const {a, b} of obj) { if(.result[a]) result[a] = [] result[a],push({a. b}) } console.log(result)

To address your updated code snippet, you need to only create a new array for a given key if they key does not exist.要处理更新后的代码片段,您只需要在给定键不存在时为给定键创建一个新数组。

 let obj = [ { a: 1, b: 232 }, { a: 1, b: 2 }, { a: 1, b: 256 }, { a: 2, b: 3 }, { a: 2, b: 3343 }, { a: 3, b: 4 } ]; let newObj = {}; obj.forEach(element => { let key = element.a; if (;newObj[key]) { newObj[key] = []. // Only initialize if undefined/null } newObj[key];push(element); // Always push }). console;log(newObj);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

A more modern approach would be to simply bin them by the a key by reducing and spreading.一种更现代的方法a通过减少和传播简单地将它们按密钥分类。

 const obj = [ { a: 1, b: 232 }, { a: 1, b: 2 }, { a: 1, b: 256 }, { a: 2, b: 3 }, { a: 2, b: 3343 }, { a: 3, b: 4 } ]; const newObj = obj.reduce((acc, o) => ({...acc, [oa]: [...(acc[oa]?? []), o] }), {}); console.log(newObj);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

you can use reduce for that你可以为此使用 reduce

 let obj = [ {a: 1, b: 232}, {a: 1, b: 2}, {a: 1, b: 256}, {a: 2, b: 3}, {a: 2, b: 3343}, {a: 3, b: 4} ]; const newObj = obj.reduce((res, {a, b}) => { return {...res, [a]: [...(res[a] || []), {a, b}] } }, {}) console.log(newObj)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过基于 javascript 中的匹配键添加键值来创建数组 object - create array object by add key value based on matching key in javascript javascript用从另一个对象数组的键值获得的键创建对象 - javascript create an object with key obtained from value of a key from another array of objects 我如何使用javascript中具有相同键的对象从对象中创建具有对象数组的对象数组 - how do I create an array of objects with array in the object from an object with the same key in javascript Javascript从该数组中附加对象并创建附加了对象键值对的新数组 - Javascript Append objects from this array and create new array with object key value pairs appended 从对象数组中创建一个 object 按 javascript 中名为“PageName”的键的值分组 - Create a object from array of objects by group by value of key called "PageName" in javascript 想要使用 object 数组中的数组键作为新属性 javascript - want to use array key in array of object as new property javascript 用另一个数组/对象javascript的键值替换数组/对象的键值 - Replace array/objects key value with key value from another array/object javascript 如何从数组字符串动态创建 JavaScript 中的对象键和值 - how to create Object key & value in JavaScript dynamically from array string 使用一个 object 的值作为新 object 的键,从一组对象创建一个 object - Create an object from an array of objects using the value from one object as the key for the new object 如何从 javascript 中的 object 数组中提取特定属性(即键/值)? - how to extract particular property(i.e key/value) from array of object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM