2

Is there a simple way to only return a few keys in my case for example i want to only return headername, field and type if it exists. I know i can delete keys via

for (i = 0; i < obj.length; i++) {
  delete obj[i]['type']
  delete obj[i]['hide']
  delete obj[i]['position']
}

which would mean i would have to loop over all docs in array and delete the keys but not sure if there is a simpler faster way to achieve the same. In my case this doc will never be huge as it only stores column definitions for export of data

[   {
             "headerName": "assessed_combined_value",
             "field": "assessed_combined_value",
             "hide": false,
             "position": 8,
             "type": "money"
        },
        {
             "headerName": "assessee1",
             "field": "assessee1",
             "hide": false,
             "position": 1
        },
        {
             "headerName": "assessee2",
             "field": "assessee2",
             "hide": false,
             "position": 2
        },
        {
             "headerName": "bathrooms",
             "field": "bathrooms",
             "hide": false,
             "position": 5
        }
        ]
1
  • how is the data coming to you? If you control the query to the db (if there is a db) you can not select the field Commented Jun 17, 2019 at 17:55

2 Answers 2

3

You can use destructuring

let data = [{"headerName": "assessed_combined_value","field": "assessed_combined_value","hide": false,"position": 8,"type": "money"},{"headerName": "assessee1","field": "assessee1","hide": false,"position": 1},{"headerName": "assessee2","field": "assessee2","hide": false,"position": 2},{"headerName": "bathrooms","field": "bathrooms","hide": false,"position": 5}]

let final = data.map(({field, headerName}) => ({headerName, field}))

console.log(final)

If the case is vice-versa you want to leave only some of properties behind and want to select all other properties you can use spread syntax i.e if you want to leave out only field key

let final = data.map(({field, ...rest}) => rest)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the spread operator combining with the previously posted answer to show only those fields which are present in the currently iterated object.

let info = [{
    "headerName": "assessed_combined_value",
    "field": "assessed_combined_value",
    "hide": false,
    "position": 8,
    "type": "money"
},
{
    "headerName": "assessee1",
    "field": "assessee1",
    "hide": false,
    "position": 1
},
{
    "headerName": "assessee2",
    "field": "assessee2",
    "hide": false,
    "position": 2
},
{
    "headerName": "bathrooms",
    "field": "bathrooms",
    "hide": false,
    "position": 5
}]

const result = info.map(({field, headerName, type}) => ({
    ...(headerName && {"headerName": headerName}),
    ...(field && {"field": field}),
    ...(type && {"type": type})
}));
console.log(result);

2 Comments

So is there a significant benefit by using one vs the other specially if the Docs and Arrays are not to big like 10 docs in array and maybe 20 - 40 keys in a doc.
You won't get {type: undefined} properties if you use my example. Only in those cases, when the original object had the type property with a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.