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 badges
Add a ment  | 

3 Answers 3

Reset to default 7

Use 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