映射两个数组
Mapping two arrays
我有两个数组。
let A = [{
"id": 22,
"name": "ABC",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}, {
"id": 23,
"name": "DEF",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}, {
"id": 24,
"name": "GHI",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}]
let B = [{
"key": "selectedId",
"value": "id"
}, {
"key": "selectedName",
"value": "name"
}]
我想映射这两个数组。就像下面的数组一样。
let C = [{
selectedId: 22,
selectedName: "ABC"
}, {
selectedId: 23,
selectedName: "DEF"
}, {
selectedId: 24,
selectedName: "GHI"
}]
下面显示了我尝试过的内容,所以 far.I 不知道如何像上面的数组一样映射数据。如果有人能回答这个问题,那将是一个巨大的帮助。
A.forEach(item => {
B.forEach(element => {
});
});
您可以将条目映射到想要的 key/value 对。
const
data = [{ id: 22, name: "ABC", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null }, { id: 23, name: "DEF", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null }, { id: 24, name: "GHI", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null}],
keys = [{ key: "selectedId", value: "id" }, { key: "selectedName", value: "name" }],
result = data.map(o =>
Object.fromEntries(keys.map(({ key, value }) => [key, o[value]]))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const
A = [
{"id":22,"name":"ABC","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null},
{"id":23,"name":"DEF","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null},
{"id":24,"name":"GHI","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null}
],
B = [
{"key":"selectedId","value":"id"},
{"key":"selectedName","value":"name"}
];
// get map of prop-key from B
const map = B.reduce((map, {key,value}) => map.set(value, key), new Map);
// iterate over A
const res = A.map(item =>
// from the current item, construct an object using the above map
[...map.entries()].reduce((obj, [prop, key]) => ({...obj, [key]: item[prop]}), {})
);
console.log(res);
您正在将一个数组转换为另一个长度相同的数组,所以是的 - 这是一个映射操作。在其中,您正在将数组转换为……其他东西。这是一个减少。
这是我的方法:
let A = [
{
id: 22,
name: "ABC",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
{
id: 23,
name: "DEF",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
{
id: 24,
name: "GHI",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
];
let B = [
{
key: "selectedId",
value: "id",
},
{
key: "selectedName",
value: "name",
},
];
const output = A.map((item) =>
B.reduce((accum, pair) => ({ ...accum, [pair.key]: item[pair.value] }), {})
);
console.log(output);
我有两个数组。
let A = [{
"id": 22,
"name": "ABC",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}, {
"id": 23,
"name": "DEF",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}, {
"id": 24,
"name": "GHI",
"createdDate": "2021-04-19T05:55:41.247+0000",
"modifiedDate": null
}]
let B = [{
"key": "selectedId",
"value": "id"
}, {
"key": "selectedName",
"value": "name"
}]
我想映射这两个数组。就像下面的数组一样。
let C = [{
selectedId: 22,
selectedName: "ABC"
}, {
selectedId: 23,
selectedName: "DEF"
}, {
selectedId: 24,
selectedName: "GHI"
}]
下面显示了我尝试过的内容,所以 far.I 不知道如何像上面的数组一样映射数据。如果有人能回答这个问题,那将是一个巨大的帮助。
A.forEach(item => {
B.forEach(element => {
});
});
您可以将条目映射到想要的 key/value 对。
const
data = [{ id: 22, name: "ABC", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null }, { id: 23, name: "DEF", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null }, { id: 24, name: "GHI", createdDate: "2021-04-19T05:55:41.247+0000", modifiedDate: null}],
keys = [{ key: "selectedId", value: "id" }, { key: "selectedName", value: "name" }],
result = data.map(o =>
Object.fromEntries(keys.map(({ key, value }) => [key, o[value]]))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const
A = [
{"id":22,"name":"ABC","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null},
{"id":23,"name":"DEF","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null},
{"id":24,"name":"GHI","createdDate":"2021-04-19T05:55:41.247+0000","modifiedDate":null}
],
B = [
{"key":"selectedId","value":"id"},
{"key":"selectedName","value":"name"}
];
// get map of prop-key from B
const map = B.reduce((map, {key,value}) => map.set(value, key), new Map);
// iterate over A
const res = A.map(item =>
// from the current item, construct an object using the above map
[...map.entries()].reduce((obj, [prop, key]) => ({...obj, [key]: item[prop]}), {})
);
console.log(res);
您正在将一个数组转换为另一个长度相同的数组,所以是的 - 这是一个映射操作。在其中,您正在将数组转换为……其他东西。这是一个减少。
这是我的方法:
let A = [
{
id: 22,
name: "ABC",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
{
id: 23,
name: "DEF",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
{
id: 24,
name: "GHI",
createdDate: "2021-04-19T05:55:41.247+0000",
modifiedDate: null,
},
];
let B = [
{
key: "selectedId",
value: "id",
},
{
key: "selectedName",
value: "name",
},
];
const output = A.map((item) =>
B.reduce((accum, pair) => ({ ...accum, [pair.key]: item[pair.value] }), {})
);
console.log(output);