ServerMonkey
ServerMonkey

Reputation: 1154

Removing nested arrays

I have an array that contains a lot of base attributes and multiple nested arrays. I'm trying to remove the nested arrays leaving all the base items. I don't want to filter them out by name but type.

{
    "ID": 123,
    "ItemName": "Item1",
    "CommentList": [
        {
            "CommentID": "ABC",
            "CommentText": "Test entry"
        }
    ],
    "ExtraList": [
        "A123B"
    ]
}

The desired outcome is:

{
    "ID": 123,
    "ItemName": "Item1",
}

I tried filter but couldnt get it to work, the below is the closest I've come but given the fact I can't gracefully return the property back to the caller this must be the wrong track.

  let test = arr.forEach(function (item) {
    for (var propertyName in item) {
      if (!Array.isArray(propertyName))
        return propertyName;    
    }
  });

Upvotes: 1

Views: 81

Answers (4)

Narendra Chouhan
Narendra Chouhan

Reputation: 2319

You can Do this way, The easiest way to Do this

const data = [
{
    "ID": 123,
    "ItemName": "Item1",
    "CommentList": [
        {
            "CommentID": "ABC",
            "CommentText": "Test entry"
        }
    ],
    "ExtraList": [
        "A123B"
    ]
},
{
    "ID": 124,
    "ItemName": "Item2",
    "CommentList": [
        {
            "CommentID": "CDE",
            "CommentText": "Test entry 2"
        }
    ],
    "ExtraList": [
        "A123BC"
    ]
}
]

let finalOutput=JSON.parse(JSON.stringify(data,['ID','ItemName']))
console.log(finalOutput)

Upvotes: 0

javascwipt
javascwipt

Reputation: 1178

For starters, Array.prototype.forEach does not return a new array. Please use Array.prototype.map instead if you would like to return a new array.

I am only saying this because you are assigning it to test, which would return undefined.

With that out of they way this should work

data.forEach(item => {
for(const key in item) {
    if(Array.isArray(item[key])) {
        delete item[key]        
    }
}
})

Here is the code that input and output:

Input:

const data = [
{
    "ID": 123,
    "ItemName": "Item1",
    "CommentList": [
        {
            "CommentID": "ABC",
            "CommentText": "Test entry"
        }
    ],
    "ExtraList": [
        "A123B"
    ]
},
{
    "ID": 124,
    "ItemName": "Item1",
    "CommentList": [
        {
            "CommentID": "ABC",
            "CommentText": "Test entry"
        }
    ],
    "ExtraList": [
        "A123B"
    ]
}
]

Output:

[
{ID: 123, ItemName: "Item1"},
{ID: 124, ItemName: "Item1"}
]

Hope this helps!

Upvotes: 1

Hans Felix Ramos
Hans Felix Ramos

Reputation: 4434

arr is an object, you can get an arrays of keys with Object.keys(obj) an then filter it.

let obj = {
  "ID": 123,
  "ItemName": "Item1",
  "CommentList": [{
    "CommentID": "ABC",
    "CommentText": "Test entry"
  }],
  "ExtraList": [
    "A123B"
  ]
}

let test = Object.keys(obj).filter((key) => {
  return !Array.isArray(obj[key])
})

console.log(test)

Upvotes: 0

Shijil Narayanan
Shijil Narayanan

Reputation: 1019

Try this

const a = {
  "ID": 123,
  "ItemName": "Item1",
  "CommentList": [
    {
        "CommentID": "ABC",
        "CommentText": "Test entry"
    }
 ],
 "ExtraList": [
    "A123B"
 ]
}

const obj = {};

Object.keys(a)
.filter(key => !(a[key] instanceof Array))
.forEach(key => obj[key] = a[key]);

console.log(obj);

Upvotes: 1

Related Questions