0

I want to access id's inside the recordId

[ { recordId:
     [ 5d836ceb2afc1764751f8d8c,
       5d836ceb2afc1764751f8d92,
       5d836ceb2afc1764751f8d95 ],
    isDelete: false,
    _id: 5ec278f6da31482240554476,
    createdAt: 2020-05-18T12:00:54.355Z,
    updatedAt: 2020-05-18T12:00:54.355Z,
    __v: 0 },

  { recordId:
     [ 5d836ceb2afc1764751f8d8c,
       5d836ceb2afc1764751f8d92,
       5d836ceb2afc1764751f8d95 ],
    isDelete: false,
    _id: 5ec276334e094223c84a398e,
    createdAt: 2020-05-18T11:49:07.691Z,
    updatedAt: 2020-05-18T11:49:07.691Z,
    __v: 0 },
...
]

2 Answers 2

2

The structure of the object that you've shared looks like

let obj = [
   { 
      recordId : [ ]
      ...
   }
.....
]

One way to get the element will be using the index for the outside array and then for the nested array.

obj[ <index> ].recordId[ <index inside recordId> ] 

You can use the loops to get access too. Taking an example

let obj = [
{
      "recordId":[
         "6d836ceb2afc1764751f8d8c",
         "6d836ceb2afc1764751f8d92",
         "6d836ceb2afc1764751f8d95"
      ],
      "isDelete":false,
      "_id":"5ec26f964cf6ec0520f5e53a",
      "createdAt":"2020-05-18T11:20:54.939Z",
      "updatedAt":"2020-05-18T11:20:54.939Z",
      "__v":0
   }
]

The following code gives access to each of the object in the parent array

obj.forEach(element => {
    console.log(element);
})

Now to get access of the inside recordId, following code can be used

obj.forEach(element => {
    element.recordId.forEach(id =>{
      console.log(id);
    })
})

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This is very good for a first post. I especially appreciate the combination of explanation and code. Thank you.
Thank you for the feedback Jeremy
1
dataList = [
    { recordId: [`5d836ceb2afc1764751f8d8c`, `5d836ceb2afc1764751f8d92`, `5d836ceb2afc1764751f8d95`], isDelete: false, _id: `5ec278f6da31482240554476`, createdAt: `2020-05-18T12:00:54.355Z`, updatedAt: `2020-05-18T12:00:54.355Z`, __v: 0 },
    { recordId: [`5d836ceb2afc1764751f8d8c`, `5d836ceb2afc1764751f8d92`, `5d836ceb2afc1764751f8d95`], isDelete: false, _id: `5ec278f6da31482240554476`, createdAt: `2020-05-18T12:00:54.355Z`, updatedAt: `2020-05-18T12:00:54.355Z`, __v: 0 },
    { recordId: [`5d836ceb2afc1764751f8d8c`, `5d836ceb2afc1764751f8d92`, `5d836ceb2afc1764751f8d95`], isDelete: false, _id: `5ec278f6da31482240554476`, createdAt: `2020-05-18T12:00:54.355Z`, updatedAt: `2020-05-18T12:00:54.355Z`, __v: 0 },
]

dataList.forEach(item => {
    item.recordId.forEach(id_Item => {
        console.log(id_Item)
    });
});

Comments

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.