在每个对象中使用Object.assign()

时间:2018-06-08 09:40:37

标签: javascript arrays object

我不知道为什么在某些项目的结果中,价值是错误的。

var myArray = [{
    item: "my apple 1",
    value: 1
}, {
    item: "my carrot",
    value: 2
}, {
    item: "my banana",
    value: 3
}, {
    item: "my potatoe",
    value: 4
}, {
    item: "my apple 2",
    value: 0
}];


var MyArrayDefinition = [{
    item: "my apple 1",
    color: "red",
    group: "fruit",
    score: 1
}, {
    item: "my carrot",
    color: "orange",
    group: "vegetable",
    score: 0
}, {
    item: "my banana",
    color: "yellow",
    group: "fruit",
    score: 1
}, {
    item: "my apple 2",
    color: "red",
    group: "fruit",
    score: 0
}, {
    item: "my potatoe",
    color: "yellow",
    group: "vegetable",
    score: 0
}, ]

这是我的功能,但结果对于项目来说很奇怪"我的苹果2"

    var result = [];
    myArray.forEach((itm, i) => {
    result.push(Object.assign({}, itm, MyArrayDefinition[i]));

我喜欢

[{
item: "my apple 1",
color: "red",
group: "fruit",
score: 1,
value: 1
}, {
item: "my carrot",
color: "orange",
group: "vegetable",
score: 0,
value: 2
}, {
item: "my banana",
color: "yellow",
group: "fruit",
score: 1,
value: 3
}, {
item: "my apple 2",
color: "red",
group: "fruit",
score: 0,
value: 0
}, {
item: "my potatoe",
color: "yellow",
group: "vegetable",
score: 0,
value: 4
}];

3 个答案:

答案 0 :(得分:1)

只有两个数组具有完全相同的顺序和长度时,您的代码才有效。要使其灵活,请使用find()并按项属性匹配第二个对象。

const myArray = [{item:"my apple 1", value:1},{item:"my carrot", value:2},{item:"my banana", value:3},{item:"my potatoe", value:4},{item:"my apple 2", value:0}];
const MyArrayDefinition = [{item:"my apple 1", color:"red", group:"fruit", score:1},{item:"my carrot", color:"orange", group:"vegetable", score:0},{item:"my banana", color:"yellow", group:"fruit", score:1},{item:"my apple 2", color:"red", group:"fruit", score:0},{item:"my potatoe", color:"yellow", group:"vegetable", score:0},]

const combined = myArray.map(e => Object.assign(e, MyArrayDefinition.find(x => x.item === e.item)));
console.log(combined);

如果两个数组都很大,请考虑首先索引MyArrayDefinition:

const myArray = [{item:"my apple 1", value:1},{item:"my carrot", value:2},{item:"my banana", value:3},{item:"my potatoe", value:4},{item:"my apple 2", value:0}];
const MyArrayDefinition = [{item:"my apple 1", color:"red", group:"fruit", score:1},{item:"my carrot", color:"orange", group:"vegetable", score:0},{item:"my banana", color:"yellow", group:"fruit", score:1},{item:"my apple 2", color:"red", group:"fruit", score:0},{item:"my potatoe", color:"yellow", group:"vegetable", score:0},]


const indices = MyArrayDefinition.map((e) => e.item);
const comb = myArray.map(e => Object.assign(e, MyArrayDefinition[indices.indexOf(e.item)]));
console.log(comb);

即使对于小型阵列来说似乎也要快一些,因此您应该从现实世界的场景中受益。 Check performance here

答案 1 :(得分:1)

您的合并代码段假定具有匹配项的数组。 I.e。:my apple 2myArray的{​​{1}}索引必须相同。

如果你想支持不同的集合并且仍然正确合并,你可以创建一个有点像MyArrayDefinition的函数。逻辑中的两个重要步骤是:

  1. 是什么让两个对象适合合并?
    在您的情况下: groupBy

  2. 我们如何合并两个对象?
    在您的情况下:(a, b) => a.item === b.item

  3. 现在,我们可以(天真地)减少a, b) => Object.assign({}, a, b})而不用担心订单:

    
    
    myArray
    
    
    

    请注意,此函数效率仍然非常低,因为它在每个循环中都使用var myArray=[{item:"my apple 1",value:1},{item:"my carrot",value:2},{item:"my banana",value:3},{item:"my potatoe",value:4},{item:"my apple 2",value:0}]; var MyArrayDefinition = [{ item: "my apple 1", color: "red", group: "fruit", score: 1 }, { item: "my carrot", color: "orange", group: "vegetable", score: 0 }, { item: "my banana", color: "yellow", group: "fruit", score: 1 }, { item: "my apple 2", color: "red", group: "fruit", score: 0 }, { item: "my potatoe", color: "yellow", group: "vegetable", score: 0 } ]; var merged = myArray.map( (x) => { const pair = MyArrayDefinition.find(y => x.item === y.item); return (Object.assign({}, x, pair || {})); }); console.log(merged);。如果您需要演奏,可以先按find键为项目编制索引。

答案 2 :(得分:-1)

var result = [];
myArray.forEach((itm, i) => {
result.push(Object.assign({}, itm, MyArrayDefinition[i]));

在上面的行中,您只需要在Object.assign方法中替换itm和MyArrayDefinition [i]。

像这样,

evar result = [];
myArray.forEach((itm, i) => {
result.push(Object.assign({}, MyArrayDefinition[i],itm));