const productsMaster = Immutable.fromJS([{
"bathrooms": "2.5",
"price": 309990,
"name": "Costa Verde",
"garages": {
"config": null,
"number": "2"
},
"stories": 1,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 309990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 309990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 307990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "3",
"id": "41D0F85C-4798-465C-B5AA-F4B6F7F56901",
"finishedSqFt": 2107,
"isModel": false
}, {
"bathrooms": "2",
"price": 281990,
"name": "La Mancha",
"garages": {
"config": null,
"number": "2"
},
"stories": 1,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 281990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 281990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 279990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "3",
"id": "A0D296AC-B5E8-4D21-9155-D2A0B4536B48",
"finishedSqFt": 1826,
"isModel": false
}, {
"bathrooms": "2",
"price": 289990,
"name": "Madrid",
"garages": {
"config": null,
"number": "2"
},
"stories": 1,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 289990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 289990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 287990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "3",
"id": "5BE28BA0-DD4E-449B-8814-BBD9D44541B8",
"finishedSqFt": 2005,
"isModel": false
}, {
"bathrooms": "3",
"price": 349990,
"name": "Residence 5",
"garages": {
"config": null,
"number": "3"
},
"stories": 2,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 349990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 349990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 349990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "3",
"id": "EE791214-023F-49BC-BB39-04707ACB35EE",
"finishedSqFt": 2391,
"isModel": false
}, {
"bathrooms": "3",
"price": 364990,
"name": "Residence 6",
"garages": {
"config": null,
"number": "3"
},
"stories": 2,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 364990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 364990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 364990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "4",
"id": "0B8472E6-DE56-4E32-ADD0-39EA392DBE53",
"finishedSqFt": 2579,
"isModel": false
}, {
"bathrooms": "2",
"price": 259990,
"name": "Santona",
"garages": {
"config": null,
"number": "2"
},
"stories": 1,
"history": [{
"date": "2017-09-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 259990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-06-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 259990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}, {
"date": "2017-03-30T00:00:00",
"closings": {
"qtr": 0,
"total": 0
},
"starts": {
"qtr": 0,
"total": 0
},
"price": 259990,
"units": {
"released": null,
"available": null,
"sold": null,
"inventory": null
}
}],
"bedrooms": "3",
"id": "237CA02C-1C66-4182-8BEC-D66D6B2AF500",
"finishedSqFt": 1601,
"isModel": false
}])
const products = Immutable.fromJS([{
"unitsSold": 5,
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 309990,
"unitsPlanned": 5,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "E9273001-B2CB-4057-943D-0384ED5BC00D",
"finishedSqFt": ""
}, {
"unitsSold": 0,
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 280990,
"unitsPlanned": 10,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "BEE1E6B1-968E-4A25-A74F-731B8CD15E17",
"finishedSqFt": ""
}, {
"unitsSold": 4,
"bestSeller": {
"checked": true,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": true,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 292990,
"unitsPlanned": 5,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "E1CB9E4F-B634-4043-B4D4-BAB441F47766",
"finishedSqFt": ""
}, {
"unitsSold": 2,
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 340990,
"unitsPlanned": 3,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "EB0E0343-1BDD-4DD3-BA1A-CF0625F3CDF6",
"finishedSqFt": ""
}, {
"unitsSold": 2,
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 362990,
"unitsPlanned": 2,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "41D0F85C-4798-465C-B5AA-F4B6F7F56901",
"finishedSqFt": ""
}, {
"unitsSold": 3,
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": 261990,
"unitsPlanned": 5,
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "A0D296AC-B5E8-4D21-9155-D2A0B4536B48",
"finishedSqFt": ""
}, {
"unitsSold": "",
"bestSeller": {
"checked": false,
"label": "",
"value": "bestSeller"
},
"model": {
"checked": false,
"label": "",
"value": "model"
},
"productId": "",
"name": "",
"auditPrice": "",
"unitsPlanned": "",
"notAvailable": {
"checked": false,
"label": "",
"value": "notAvailable"
},
"id": "5BE28BA0-DD4E-449B-8814-BBD9D44541B8",
"finishedSqFt": ""
}])
function combineProducts(productsMaster, products) {
let _products = []
productsMaster.forEach(item => {
const _prod = products.find(x => x.get('id') === item.get('id'))
_products.push({
name: item.get('name'),
id: item.get('id'),
finishedSqFt: item.get('finishedSqFt') || '',
auditPrice: _prod ? _prod.get('auditPrice') : 0,
unitsPlanned: _prod ? _prod.get('unitsPlanned') : 0,
unitsSold: _prod ? _prod.get('unitsSold') : 0,
model: {
label: '',
value: 'model',
checked: _prod ? _prod.getIn(['model', 'checked']) : false
},
bestSeller: {
label: '',
value: 'bestSeller',
checked: _prod ? _prod.getIn(['bestSeller', 'checked']) : false
},
notAvailable: {
label: '',
value: 'notAvailable',
checked: _prod ? _prod.getIn(['notAvailable', 'checked']) : false
}
})
})
return _products
}
const result = combineProducts(productsMaster, products)
console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
&#13;
我需要通过ID比较两个对象数组,并在新数组中将属性合并在一起。我使用的是我一直使用的相同函数,一个forEach循环中的find函数。但结果并不是理想的结果。只有2个ID匹配组合对象。这个函数与我使用这个逻辑的其他函数之间的区别是我需要最终结果与主数组的长度相同。