0
var cart =
 [
 {
    "Items": "",
    "category": "",
    "contents":
 [
 {
    "Apple iPhone": "222",
    "French": "Bounjour",
    "id": 1234,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"false"
 }
 ]
 }, 
 {
    "Items": "No 2",
    "category": "2nd",
    "contents":
 [
 {
    "redmi": "333",
    "French": "some text",
    "id": 345787,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"true"
 }, 
 {
    "samsung": "333",
    "French": "some text",
    "id": 86787876,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"disabled"
 }
 ]
 }
 ];

Can anyone help me to get the "id" value and "pricetag" value from the above JSON ? It is nested one and can be in different format. But in each, i need to extract id value and pricetag value. I tried so many things but not getting exact output. Can someone please help me ? Sorry for the bad formatting ..

  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – JJJ Apr 14 '18 at 20:58
  • Possible duplicate of [Find by key deep in a nested object](https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-object) – JoshG Apr 14 '18 at 20:58
  • Also, that is not JSON and jQuery is a DOM manipulation library, it can't help you with this. – JJJ Apr 14 '18 at 20:59
  • So how do i get the values ? – sonu kalkar Apr 14 '18 at 21:01
  • You read the duplicates and apply that knowledge to this specific case. – JJJ Apr 14 '18 at 21:07
  • To get the id/pricetag, you need first to parse your JSON. https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/JSON/parse After that, you can access to the properties you want. –  Apr 14 '18 at 21:01

2 Answers2

1

use .map() to iterate and return the desired keys :

var cart = [{
    "Items": "",
    "category": "",
    "contents": [{
      "Apple iPhone": "222",
      "French": "Bounjour",
      "id": 1234,
      "icon": "/images/bg.jpg",
      "callback": "use()",
      "pricetag": "false"
    }]
  },
  {
    "Items": "No 2",
    "category": "2nd",
    "contents": [{
        "redmi": "333",
        "French": "some text",
        "id": 345787,
        "icon": "/images/bg.jpg",
        "callback": "use()",
        "pricetag": "true"
      },
      {
        "samsung": "333",
        "French": "some text",
        "id": 86787876,
        "icon": "/images/bg.jpg",
        "callback": "use()",
        "pricetag": "disabled"
      }
    ]
  }
];


let values = cart.map((e) => {
  return e.contents.map((a) => {
    return {
      id: a.id,
      pricetag: a.pricetag
    }
  })
})

console.log(values)
Taki
  • 17,320
  • 4
  • 26
  • 47
  • So this stores in an array if i am right ? My actual requirement is (if id value is somenumber and pricetag is somevalue) then do something . What is the better way to do that ? – sonu kalkar Apr 14 '18 at 21:09
1

This should work for you :

   var items=[];
   var some_var;
   const map1 = cart.map(function(item){

     const map2=item.contents.map(function(contentItem){
        //some_var=contentItem.id;
       return{
          pricetag:contentItem.pricetag,
          id:contentItem.id
        }
     })
     items=items.concat(map2)
 });

console.log(items);

Sample output:

[ { pricetag: 'false', id: 1234 },
  { pricetag: 'true', id: 345787 },
  { pricetag: 'disabled', id: 86787876 } ]
Deepak
  • 850
  • 5
  • 13
  • So this stores in an array if i am right ? My actual requirement is (if id value is somenumber and pricetag is somevalue) then do something . What is the better way to do that ? – sonu kalkar Apr 14 '18 at 21:14
  • Please elaborate what you actually need to do with those. Else, You can also do that inside the map2-iteration. – Deepak Apr 14 '18 at 21:16
  • Maybe store each array values in an vairable so that i can access it. – sonu kalkar Apr 14 '18 at 21:21
  • You can use the values inside the function however you wish. Like i declared some_var. You can expose the values like these variables. Do you want to do this way I guess ? – Deepak Apr 14 '18 at 21:24