admin管理员组文章数量:1399839
I have an array of objects and I need a way to allow the user to select which properties they want to import in the database. Is there a way to map and create a separate array only with the properties the user actually wants to send insert.
For example, if we have the following array:
[
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
]
and the user selects name and phone only, then the array that is sent to be added in the database should look like this:
[
{name: 'name1', phone: '123'},
{name: 'name2', phone: '123'},
{name: 'name3', phone: '123'},
{name: 'name4', phone: '123'},
]
How can this be achieved?
I have an array of objects and I need a way to allow the user to select which properties they want to import in the database. Is there a way to map and create a separate array only with the properties the user actually wants to send insert.
For example, if we have the following array:
[
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
]
and the user selects name and phone only, then the array that is sent to be added in the database should look like this:
[
{name: 'name1', phone: '123'},
{name: 'name2', phone: '123'},
{name: 'name3', phone: '123'},
{name: 'name4', phone: '123'},
]
How can this be achieved?
Share Improve this question asked Nov 9, 2020 at 16:40 RocshyRocshy 3,50912 gold badges43 silver badges58 bronze badges3 Answers
Reset to default 7Use map and return the new object
const arr = [
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
];
const res = arr.map(({name, phone}) => ({name, phone}));
console.log(res);
If you want to make it dynamic with an array of props to copy over
const arr = [
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
];
const copy = ['name', 'phone'];
const res = arr.map(data => copy.reduce((o, k) => (o[k] = data[k], o), {}));
console.log(res);
Simply use array.map function
let arr = [
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
]
let res = arr.map((x) => {
return {
name: x.name,
phone: x.phone
}
});
console.log(res)
If you need to work with puted column values like for example ['name', 'phone']
, You can create a helper function called pick
to create objects with a subset of all properties:
function pick(props, object) {
const result = {};
for (const prop of props) {
if (prop in object) {
result[prop] = object[prop];
}
}
return result;
}
Now you can pass an array of keys, and the source object, and pick
will return a partial object with only the specified keys:
const object = { name: 'foo', address: 'bar', phone: 'baz' };
console.log(
pick(['name', 'phone'], object)
);
//////////////////////////////////////////////////
function pick(props, object) { const result = {}; for (const prop of props) { if (prop in object) { result[prop] = object[prop]; } } return result; }
If you need to do this on an array of objects, use Array.map
together with pick
like this:
const arr = [
{name: 'name1', address: 'addr1', phone: '123'},
{name: 'name2', address: 'addr1', phone: '123'},
{name: 'name3', address: 'addr1', phone: '123'},
{name: 'name4', address: 'addr1', phone: '123'},
];
console.log(
arr.map((obj) => pick(['name', 'phone'], obj))
);
//////////////////////////////////////////////////
function pick(props, object) { const result = {}; for (const prop of props) { if (prop in object) { result[prop] = object[prop]; } } return result; }
本文标签: javascriptTypescript map specific columns from an arrayStack Overflow
版权声明:本文标题:javascript - Typescript map specific columns from an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744224536a2596025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论