【发布时间】:2018-11-20 18:03:23
【问题描述】:
我想按名称过滤 myArray 键包含字符串“item”(所有“item1”、“item2”、“item3”等)
const combined = myArray.map(e => Object.assign(e, MyArrayDefinition.find(k => k.item === e.item)));
我已经用 .includes 尝试过这个,但它不起作用
const combined = myArray.map(e => Object.assign(e, MyArrayDefinition.find(k => k.includes('item') === e.item)));
我的数组
const myArray = [{
"shop": "shop1",
"item1": "my apple 1",
"item2": "my carrot 1",
}, {
"shop": "shop2",
"item1": "my apple 0",
"item2": "my carrot 1",
}, {
"shop": "shop2",
"item1": "my apple 1",
"item2": "my carrot 0",
}, ];
const MyArrayDefinition = [ {
"item": "my apple 0",
"color": "red",
"group": "my fruit",
"score": 0
}, {
"item": "my carrot 1",
"color": "orange",
"group": "my vegetable",
"score": 1
}, {
"item": "my apple 1",
"color": "red",
"group": "my fruit",
"score": 1
}, {
"item": "my carrot 0",
"color": "orange",
"group": "my vegetable",
"score": 0
}];
【问题讨论】:
-
请添加想要的结果...
-
您是在比较
item1与item还是item2与item? -
item2会发生什么? -
我将 item1 和 item2 与 item 进行比较以获取它们的颜色和组
-
请将结果添加到问题中并添加为什么项目是一项而不是另一项。
标签: javascript arrays object filter include