简体   繁体   English

在forEach循环中使用动态键

[英]Using dynamic keys in forEach loop

I have the following javascript Object 我有以下javascript对象

const items = {
    0: {
        id: 1,
        color: 'blue'
    },
    1: {
        id: 2,
        color: 'red'
    }
}

I am trying to restructure it so that the keys have an indexed numerical value like so: item1id , item1color , item2id , item2color . 我正在尝试对其进行重组,以使键具有类似这样的索引数字值: item1iditem1coloritem2iditem2color Or to be more precise it should look like this in the end: 或者更确切地说,它最终应该看起来像这样:

const restrucuturedItems = {
    item1id: 1,
    item1color: 'blue',
    item2id: 2,
    item2color: 'red'
}

I have tried the following, but so far didn't yield positive results: 我已经尝试了以下方法,但到目前为止并未产生积极的结果:

const restrucuturedItems = {}; // should collect the restructured data in one object like so: {item1id: 1, item1color: 'blue', item2id:2, item2color: 'red'}
const restructuredData = Object.keys(items).forEach(key => {
    let i = parseInt(key, 10) + 1;
    let item = {
        item[i]id: 1, // this part is incorrect. it should produce item1id, item2id
        item[i]color: 'blue' // this part is incorrect. it should produce item1color, item2color
    }
    restrucuturedItems.push(item);
});

After hours of research, I still don't know how to write this part correctly. 经过数小时的研究,我仍然不知道如何正确编写此部分。

You shouldn't use push because you want your restrucuturedItems to be an object , not an array. 您不应该使用push因为您希望restrucuturedItems是一个对象 ,而不是数组。

Concatenate the i with the id or color inside the brackets, and use two Object.entries - one to get the keys and values of each object in the outer object, and one to get the keys and values of each inner object: i与方括号的id或颜色连接起来,并使用两个 Object.entries一个用于获取外部对象中每个对象的键和值,另一个用于获取每个内部对象的键和值:

 const items = { 0: { id: 1, color: 'blue' }, 1: { id: 2, color: 'red' } } console.log(Object.entries(items).reduce((a, [num, obj]) => { Object.entries(obj).forEach(([key, val]) => { a['item' + num + key] = val; }); return a; }, {})); 

If you really wanted to start at item1id rather than item0id , then increment num initially: 如果您真的想从item1id而不是item0id ,那么首先增加num

 const items = { 0: { id: 1, color: 'blue' }, 1: { id: 2, color: 'red' } } console.log(Object.entries(items).reduce((a, [num, obj]) => { num++; Object.entries(obj).forEach(([key, val]) => { a['item' + num + key] = val; }); return a; }, {})); 

 const items = { 0: { id: 1, color: 'blue' }, 1: { id: 2, color: 'red' } } const restrucuturedItems = {}; // should collect the restructured data in one object like so: {item1id: 1, item1color: 'blue', item2id:2, item2color: 'red'} const restructuredData = Object.keys(items).forEach(key => { let i = parseInt(key, 10) + 1; restrucuturedItems[`item${i}id`] = 1; restrucuturedItems[`item${i}color`] = 'blue' }); console.log(restrucuturedItems) 

First of all, you cannot use push onto an object. 首先,您不能使用push对象。 push is a method meant for array. push是用于数组的方法。 Secondly, you can use template string with backtick to construct the property name that you wish 其次,可以使用带有反引号的模板字符串来构造所需的属性名称

a reduce and map function may do the job 缩小和贴图功能可以胜任

var result = Object.values(items).reduce(function (r, val) { // get all the values
  var prefix = 'item' + val.id; // construct the prefix like item1, item2
  Object.getOwnPropertyNames(val).map(function(key) {
    r[prefix + key] = val[key]; // add to the result object
  });
  return r;
}, {});

https://jsfiddle.net/vw49g8t2/7/ https://jsfiddle.net/vw49g8t2/7/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM