简体   繁体   English

Javascript 数组格式

[英]Javascript array format

I have two javascript array我有两个 javascript 阵列

let a=[{id:1},{id:2},{id:3}];
let b=[{count:35, name:'test', age:12}, {count:45 name:'test2', age:9}];

and two array push into i need final Array format is和两个数组推入我需要的最终数组格式是

[{count:35,name:'test', age,12, id:1} , {count:35, name:'test', age:12,id:2},{count:35, name:'test', age:12,id:3},{count:45,name:'test', age,9, id:1} , {count:45, name:'test', age:9,id:2},{count:45, name:'test', age:9,id:3} ]

And i trying我试着

for ( var index=0; index<b.length; index++ ) {
    
    for ( var j=0; j<a.length; j++ ) {

        for (const [key, value] of Object.entries(a[j])) {
             //if(j==index)
               b[index][key]=value;
               k[j]=b[index];

        }
        console.log( b[index]);
        c.push(b[index]);
        
    }


}
console.log(c);

and it shows final value is它显示最终值为在此处输入图像描述

please any body help to fix the problem请任何机构帮助解决问题

Your current implementation, is essentially updating the same b objects over and over, x number of times, changing their IDs each time through, ending at 3.您当前的实现本质上是一遍又一遍地更新相同的 b 对象,x 次,每次都更改它们的 ID,以 3 结束。

You need to "clone" the objects, so they are separate objects, like so:您需要“克隆”对象,因此它们是单独的对象,如下所示:

  let p = []; // Permutations
  
  let a = [{id:1},{id:2}]; // Renditions...
  // Presets...
  let b = [{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}];
  
  // for each Rendition
  a.forEach(function(a) {
     // for each Preset
     b.forEach(function(b){
         // generate Permutation and Clone, object of b with additional id prop
         p.push(Object.assign({id: a.id}, b)); // <-- Cloned here...
     });
  });

   console.log(p)

for the sake of clarity, you might consider changing the id prop to grouping or group or group_id .为了清楚起见,您可以考虑将id属性更改为groupinggroupgroup_id

You could use flatMap :您可以使用flatMap

 let a=[{id:1},{id:2},{id:3}]; let b=[{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}]; let m = b.flatMap(itemB => a.map(itemA => ({...itemB, ...itemA }))) console.log(m);

I got your problem.我有你的问题。 The thing is that you are mutating the same b object every iteration, that's why in the end you get id = 3 for every element.问题是您每次迭代都在改变相同的b object ,这就是为什么最终每个元素都会得到 id = 3 的原因。 You can use a deep copy of b in each cycle, and the error should be gone.您可以在每个循环中使用 b 的深层副本,并且错误应该消失了。

Here's one way to deep clone, not the most efficient but it is very clear:这是深度克隆的一种方法,不是最有效的,但很清楚:

        const bCopy = JSON.parse(JSON.stringify(b[index]));

        for (const [key, value] of Object.entries(a[j])) {
             //if(j==index)
               bCopy[key]=value;
               k[j]=bCopy;   // what's this for?

        }
        console.log(bCopy);
        c.push(bCopy);

Reduce b and for each item b1 , reduce a .减少b并且对于每个项目b1 ,减少a When reducing a , just assign both b1 and a1 to a new object.减少a时,只需将b1a1都分配给新的 object。

 const expectedValue = JSON.stringify(JSON.parse( document.getElementById('expected').value)) let a = [ { id: 1 }, { id: 2 }, { id: 3 } ] let b = [ { count: 35, name: 'test', age: 12 }, { count: 45, name: 'test2', age: 9 } ] let c = b.reduce((res, b1) => a.reduce((res1, a1) => [...res1, Object.assign({}, b1, a1)], res), []) console.log(JSON.stringify(c) === expectedValue)
 .as-console-wrapper { top: 0; max-height: 100%;important: } #expected { display; none; }
 <textarea id="expected"> [ { "count": 35, "name": "test", "age": 12, "id": 1 }, { "count": 35, "name": "test", "age": 12, "id": 2 }, { "count": 35, "name": "test", "age": 12, "id": 3 }, { "count": 45, "name": "test2", "age": 9, "id": 1 }, { "count": 45, "name": "test2", "age": 9, "id": 2 }, { "count": 45, "name": "test2", "age": 9, "id": 3 } ] </textarea>

Try this changes.试试这个改变。

    let a=[{id:1},{id:2},{id:3}];
    let b=[{count:35, name:'test', age:12}, {count:45, name:'test2', age:9}];
    let c=[];
    for ( var index=0; index<b.length; index++ ) {
        for ( var j=0; j<a.length; j++ ) {
         var obj=b[index];
            for (const [key, value] of Object.entries(a[j])) {
                   obj[key]=value;
            }
            c.push(obj);
        }
    }
    console.log(c);

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

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