这是我的第一个数组。
data = [
{
"id": 8651,
"value": "Abdominal pain"
},
{
"id": 8646,
"value": "Chest pain"
},
{
"id": 8642,
"value": "Cough"
}
]
这是我的第二个数组。
{
"symptoms": [
"N",
"N",
"Y"
]
}
这两个数组需要组合才能得到。
[
{
"id": 8651,
"value": "Abdominal pain",
"result": "N"
},
{
"id": 8646,
"value": "Chest pain",
"result": "N"
},
{
"id": 8642,
"value": "Cough",
"result": "Y"
}
]
如何实现这一目标。用reduce&concat尝试过,我没有得到想要的结果
答案 0 :(得分:2)
您可以使用map
函数来做到这一点:
var data = [
{
"id": 8651,
"value": "Abdominal pain"
},
{
"id": 8646,
"value": "Chest pain"
},
{
"id": 8642,
"value": "Cough"
}
]
var symptoms = {
"symptoms": [
"N",
"N",
"Y"
]
}
const mapFunction = (dataItem, index) => ({...dataItem, "result": symptoms.symptoms[index]});
const mappedResult = data.map(mapFunction)
console.log(mappedResult)
答案 1 :(得分:0)
这应该对您有帮助。
ng serve --configuration es5
答案 2 :(得分:0)
您可以为此使用 map 运算符。
import { map } from 'rxjs/operators';
let newArr = [];
data.map((item, index) => {
newArr.push(data[index]);
newArr[index]['result'] = symptoms[index];
});