简体   繁体   中英

get keys from the nested array of objects

I am looking for a function that can mutate my data ie array of object with a nested object. It will include keys that have object/array values (it should only include keys with immediate string/number/boolean values).

Example

[
{
  id: 1,
  person1: {
    firstname: "test1",
    lastname: 'singh',
    address: {
        state: "maharashtra",
    }
  }
},
{
  id: 2,
  person2: {
    firstname: "test2",
    lastname: 'rathod',
    address: {
        state: "kerala",
    }
  }
},
{
  id: 3, 
  person3: {
    firstname: "test3",
    lastname: 'gokale',
    address: {
        state: "Tamilnadu",
    }
  }
}

]

Expected output

[
{
  title: 'person1',
  value: 'person.id'
},
{
    title: 'person1',
    value: 'person.firstname'
},
{
    title: 'person1',
    value: 'person.lastname'
},
{
    title: 'person1',
    value: 'person.address'
},
{
    title: 'person1',
    value: 'person.address.state'
},
...sameforOthers

]

Basically, I need a function that will get an array and will return an array of objects as a given above as expected output

Thanks in advance

I have come up with a solution. below is the link for code sandbox for the https://codesandbox.io/s/heuristic-rubin-yy2cyy?file=/src/index.js:0-213same

 const suggestions = [
      {
        id: 1,
        person1: {
          id: "1",
          firstname: "test1",
          lastname: "singh",
          address: {
            state: "maharashtra"
          },
          attributeId: "fhgfgh"
        }
      }
    ];

const typeList = ["string", "number", "boolean"];

const getLabelValue = (itemList, initalArr, parentId) => {
  if (Array.isArray(itemList)) {
    itemList.forEach((currentItem, idx) => {
      const id = parentId ? `${parentId}.${idx}` : idx;
      if (typeList.includes(typeof currentItem)) {
        initalArr.push({
          title: id,
          value: id
        });
      } else {
        getLabelValue(currentItem, initalArr, id);
      }
    });
  } else {
    let keys = Object.keys(itemList);
    keys.forEach((currentKey) => {
      let currentItem = itemList[currentKey];
      const id = parentId ? `${parentId}.${currentKey}` : currentKey;
      if (typeList.includes(typeof currentItem)) {
        initalArr.push({
          title: id,
          value: id
        });
      } else {
        getLabelValue(currentItem, initalArr, id);
      }
    });
  }
  return initalArr;
};

console.log(">>>>>>>>>", getLabelValue(suggestions, [], ""));

Something like this?

[
{
  id: 1,
  person1: {
    firstname: "test1",
    lastname: 'singh',
    address: {
        state: "maharashtra",
    }
  }
},
{
  id: 2,
  person2: {
    firstname: "test2",
    lastname: 'rathod',
    address: {
        state: "kerala",
    }
  }
},
{
  id: 3, 
  person3: {
    firstname: "test3",
    lastname: 'gokale',
    address: {
        state: "Tamilnadu",
    }
  }
}
].map(item => {
    return {
        id: item.id,
        title: Object.keys(item)[1],
        value: item[Object.keys(item)[1]]
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM