1

I have this below object structure which is having 3 records as a sample now. But We will be having more than 20 records.

{
    "data": [{
            "datatype": "AccessoryProduct",
            "values": {
                "identifier": "access8770009prd",
                "shortdescription": "<p>Hybrid Dual Injection Cover and a Tempered Glass.<\/p>",
                "displayname": "Protection Essentials Bundle - Samsung Galaxy S9 (Clear) 822445132623"
            }
        },
        {
            "datatype": "AccessoryProduct",
            "values": {
                "identifier": "access8530068prd",
                "shortdescription": "String.class",
                "displayname": "JBL UA Flex Headphones (Gray) - 050036342735"
            }
        }, {
            "datatype": "AccessoryProduct",
            "values": {
                "identifier": "access8630012prd",
                "shortdescription": "<p>This slim case has everything you want - style and protection.<\/p>",
                "displayname": "Otterbox Symmetry Series Case - Samsung Galaxy S9 (Clear) - 660543444121"
            }
        }
    ]
}

From the above object, I need to get this below array.

identifierList = [ 'access8770009prd', 'access8530068prd', 'access8630012prd' ]

as a single dimensional array. Can anyone please provide an efficient approach.

4
  • 1
    That's not JSON object ... Commented Sep 10, 2018 at 11:22
  • Which one you are mentioning? Commented Sep 10, 2018 at 11:25
  • The same as you mention as "JSON object". From the tag-Wiki: "JSON (JavaScript Object Notation) is a textual data interchange format and language-independent. Use this tag when this text format is involved. DO NOT USE THIS TAG FOR NATIVE JAVASCRIPT OBJECTS OR JAVASCRIPT OBJECT LITERALS." Commented Sep 10, 2018 at 11:26
  • Your identifierList in your desired output is not an array but an object instead. I take it this should've been ['access8770009prd', 'access8530068prd', 'access8630012prd']? Commented Sep 10, 2018 at 11:40

2 Answers 2

1

if a is the json you have you can do the follwing

identifierList = a.data.map(x => x.values.identifier)
Sign up to request clarification or add additional context in comments.

1 Comment

OK fine. I ll try it and let u know
0

You can use map() with array destructuring to get that array of identifier.

var data = [{
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8770009prd",
      "shortdescription": "<p>Hybrid Dual Injection Cover and a Tempered Glass.<\/p>",
      "displayname": "Protection Essentials Bundle - Samsung Galaxy S9 (Clear) 822445132623"
    }
  },
  {
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8530068prd",
      "shortdescription": "String.class",
      "displayname": "JBL UA Flex Headphones (Gray) - 050036342735"
    }
  }, {
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8630012prd",
      "shortdescription": "<p>This slim case has everything you want - style and protection.<\/p>",
      "displayname": "Otterbox Symmetry Series Case - Samsung Galaxy S9 (Clear) - 660543444121"
    }
  }
];
var identifierList = data.map(({values}) => values.identifier);
console.log(identifierList);

USING forEach()

var data = [{
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8770009prd",
      "shortdescription": "<p>Hybrid Dual Injection Cover and a Tempered Glass.<\/p>",
      "displayname": "Protection Essentials Bundle - Samsung Galaxy S9 (Clear) 822445132623"
    }
  },
  {
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8530068prd",
      "shortdescription": "String.class",
      "displayname": "JBL UA Flex Headphones (Gray) - 050036342735"
    }
  }, {
    "datatype": "AccessoryProduct",
    "values": {
      "identifier": "access8630012prd",
      "shortdescription": "<p>This slim case has everything you want - style and protection.<\/p>",
      "displayname": "Otterbox Symmetry Series Case - Samsung Galaxy S9 (Clear) - 660543444121"
    }
  }
];
var identifierList = [];
data.forEach(({values}) => identifierList.push(values.identifier));
console.log(identifierList);

1 Comment

@Shanmugam glad to help

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.