admin管理员组

文章数量:1394053

I have below array of objects.

const array = [{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1",
    field2: "val2"
  }
},
{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1"
  }
}]

I need to get below output

const output = [{
  key: "field1",
  subkeys: []
},
{
  key: "field2",
  subkeys: []
},
{
  key: "field3",
  subkeys: ["field1", "field2"]
}]

I can get the top level field using this

const keys = array.map(x => Object.keys(x)[0]);

But not able to access the inner ones.

Kindly help... Thanks!!!

I have below array of objects.

const array = [{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1",
    field2: "val2"
  }
},
{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1"
  }
}]

I need to get below output

const output = [{
  key: "field1",
  subkeys: []
},
{
  key: "field2",
  subkeys: []
},
{
  key: "field3",
  subkeys: ["field1", "field2"]
}]

I can get the top level field using this

const keys = array.map(x => Object.keys(x)[0]);

But not able to access the inner ones.

Kindly help... Thanks!!!

Share Improve this question asked May 12, 2020 at 11:07 Dark KnightDark Knight 1,0936 gold badges26 silver badges50 bronze badges 5
  • 1 do you have only two levels of objects? – Nina Scholz Commented May 12, 2020 at 11:15
  • @DarkKnight: is your expected output const output = [[{key: "field1", subkeys: [] }, {key: "field2", subkeys: [] }, {key: "field3", subkeys: ["field1", "field2"] }], [{key: "field1", subkeys: [] }, {key: "field2", subkeys: [] }, {key: "field3", subkeys: ["field1"] }]] That is, an array of arrays? Or the one mentioned in the question – Dhivya Dandapani Commented May 12, 2020 at 14:56
  • @DhivyaDandapani As mentioned in the question. Not an array of array – Dark Knight Commented May 12, 2020 at 16:38
  • @DarkKnight: Thank you. But the answers that have been picked so far don't seem to return the expected answer. Are you still looking for the accurate answer? – Dhivya Dandapani Commented May 12, 2020 at 16:47
  • @DarkKnight: Could you please post a sample input-output for multi-level nested json also? I posted an answer. I could modify it based on your requirement – Dhivya Dandapani Commented May 12, 2020 at 18:10
Add a ment  | 

4 Answers 4

Reset to default 2

You can reduce an array of Object keys on the top level and then check if value for this key is object - retrieve its nested keys, otherwise just leave the empty array of subkeys

const array = [{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1",
    field2: "val2"
  }
},
{
  field1: "val1",
  field2: "val2",
  field3: {
    field1: "val1"
  }
}]

const result = array.map(item => {
  return Object.keys(item).reduce((acc, rec) => {
    if(typeof item[rec] === 'object') {
      return [...acc, {key: rec, subkeys: Object.keys(item[rec])}] 
    }
    return [...acc, {key: rec, subkeys: []}]
  }, [])
})

console.log(result)

You could map for any depth by looking to nested objects.

const
    getKeys = object => Object.keys(object).map(key => ({ 
        key, 
        subkeys: object[key] && typeof object[key] === 'object'
            ? getKeys(object[key])
            : []
    })),
    array = [{ field1: "val1", field2: "val2", field3: { field1: "val1", field2: "val2" } }, { field1: "val1", field2: "val2", field3: { field1: "val1" } }];

console.log(array.map(getKeys));
.as-console-wrapper { max-height: 100% !important; top: 0; }

This solution uses the lodash library. I have extracted the variables for better readability. This solution will work for 2 levels

const array = [
    {"field1":"val1","field2":"val2","field3":{"field1":"val1","field2":"val2"}},
    {"field1":"val1","field2":"val2","field3":{"field1":"val1","field4":"val1"}}
];

intermediateOutput = array.reduce(
  (accumulator, entry) => _.merge(accumulator, entry),
{});
// intermediateOutput = {"field1":"val1","field2":"val2","field3":{"field1":"val1","field2":"val2","field4":"val1"}}

const finalOutput = _.map(_.keys(intermediateOutput), (key) => {
  const value = intermediateOutput[key];
  return {key, subkeys: _.isObject(value) ? _.keys(value): [] }
})

console.log(finalOutput);
// finalOutput = [{"key":"field1","subkeys":[]},{"key":"field2","subkeys":[]},{"key":"field3","subkeys":["field1","field2","field4"]}]
const output = []
for (let [key, value] of Object.entries(array)) {
    const subkeys = Object.keys(value)
    output.push({key, subkeys})
}

Something like that?

本文标签: javascriptHow to get keys and sub keys from the array of objectStack Overflow