Home > Blockchain >  Getting the array name and values in Javacript Object
Getting the array name and values in Javacript Object

Time:03-11

My JSON output is similar to this below object and we have an array values as showing below :

const object1 = {
    sublists: {
        sales_order: [],
        data: [{
            "key1": "a",
            "value": 2
        }, {
            "key1": "b",
            "value": 4
        }],
        memo: [{
            "key1": "a",
            "value": 5
        }]
    }
}

I am trying to get the name of arrays and values in separate variable as showing below

Array name :data , key1 : a , value: 2
Array name :data , key1 : b , value: 4
Array name :memo, key1 : a , value: 5

here is an example code

function printValues(obj) {
        for(var k in obj) {
        //    console.log(k obj[k]);
            if(obj[k] instanceof Object) {
                printValues(obj[k]);
            } else {
                console.log(k '' obj[k] );
            };
        }
    };

printValues(object1) ;

This give me to iterate through array values but I need an array name as well.

Can someone help me on this ?

CodePudding user response:

I believe this will solve your problem. I followed the recursive nature of the code you gave and adapted it to make sure it was giving the output you desired. If you have any questions please let me know, and I'll try to address them.

function printValues(obj) {
  for (const [objKey, objValue] of Object.entries(
    obj
  )) {
    if (
      typeof objValue === 'object' &&
      !objValue.length
    ) {
      printValues(objValue);
    } else if (
      objValue !== undefined &&
      objValue.length > 0
    ) {
      for (let i = 0; i < objValue.length; i  ) {
        const { key1, value } = objValue[i];
        let str = `Array name: ${objKey} , key1: ${key1} , value: ${value}`;

        console.log(str);
      }
    }
  }
}

CodePudding user response:

The easy way to achieve the desired outcome is to pass the 'parentKey' to the recursive call:

const object1 = {sublists: {sales_order: [], data: [{"key1": "a", "value": 2 }, {"key1": "b", "value": 4 }], memo: [{"key1": "a", "value": 5 }] } };

function printValues(obj, parentName = null) {
    if (Object.prototype.toString.call(obj) === '[object Array]') {
        obj.forEach(o => console.log(`Array name: ${parentName}. Key1: ${o.key1}. Value: ${o.value}`));
    } else {
        for (let k in obj) {
            printValues(obj[k], k);
        }
    }  
};

printValues(object1) ;



Array name: data. Key1: a. Value: 2
Array name: data. Key1: b. Value: 4
Array name: memo. Key1: a. Value: 5

CodePudding user response:

try this

for (const obj in object1.sublists) {
  if (object1.sublists[obj].length == 0) continue;
  object1.sublists[obj].forEach((value) => {
    var k='';
    var v='';
    for (const prop in value) {
      if (prop.includes("value")) v = value[prop];
      else k = value[prop];
    }
    console.log(`Array name: ${obj} , key:  ${k}  value: ${v}`);
  });
};
  • Related