-2
 var s=  [{"id":"1","deleted":"0","data":[{"title":"Business Unit","value":"bus 1"},{"title":"Company ID","value":"comp 1"},{"title":"Parent ID","value":"parent 1"},{"title":"NPI","value":"npi 1"}]}];

i want to display in a table?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
minnu merin alex
  • 87
  • 1
  • 2
  • 9
  • Actually i want to iterate through this array. How will I do that? By doingJSON.stringify I am not able to loop the array – minnu merin alex Nov 26 '20 at 03:50
  • you need to access s.data[0].title, s.data[0].value, s.data[1].title, s.data[1].value... etc... – me_ Nov 26 '20 at 19:46
  • https://mkyong.com/javascript/how-to-access-json-object-in-javascript/ this explains the basic ideas – me_ Nov 26 '20 at 19:52

1 Answers1

-1

Your variable s is an array which contains one single element which is an object.

var s = [{}]

I see you have the "data" key inside the single object, whose value is an array:

"data": [{
        "title": "Business Unit",
        "value": "bus 1"
    }, {
        "title": "Company ID",
        "value": "comp 1"
    }, {
        "title": "Parent ID",
        "value": "parent 1"
    }, {
        "title": "NPI",
        "value": "npi 1"
    }]

To iterate over arrays (this is what you are asking for in your comment), you can use:

  1. for...of statement

i.e.

for (const element of s[0]['data']) {
  console.log('title', element.title);  // will output 'Business Unit' for first element
  console.log('value', element.value);  // will output 'bus 1' for first element
}
  1. forEach()

i.e.

s.forEach(element => console.log(element)); 
// will output the single object contained in var s. 
// You can access each key with dot or bracket notation (element[id]) or (element.id)

To iterate over an object keys and values, you can use Object.entries() method. i.e.

for (const [key, value] of Object.entries(s[0])) {
  console.log(`${key}: ${value}`);
  // First line will be: id: 1
}

I also advice you to check MDN documentation for any doubt regarding JavaScript.

Santironhacker
  • 722
  • 6
  • 11