JS how does a json object traverse the value of each?

clipboard.png
what should I do now to get the name value inside?

Mar.01,2021

first of all, your structure should be more appropriate as an array:

var data = [
  {name: 'asdfadfa'},
  {name: 'asdfadfa'},
  {name: 'asdfadfa'},
  {name: 'asdfadfa'},
  {name: 'asdfadfa'},
  {name: 'asdfadfa'}
];

then follow the structure you give, and traverse it like this:

for(let i in data) {
  console.log(data[i].name);
}

if you change it to an array, traverse it like this:

for(let obj of data) {
  console.log(obj.name);
}

for more group and object traversal methods, search for "js traversal array" and "js traversal object".


convert to an array with Object.keys

Object.keys(data).map(key => {
    console.log(data.key.name);
})

using ES6 syntax, one line can be solved:

var data = {
  1:{name: 'asdfadfa'},
  2:{name: 'asdfadfa'},
  3:{name: 'asdfadfa'},
  4:{name: 'asdfadfa'},
  5:{name: 'asdfadfa'},
  6:{name: 'asdfadfa'}
}

Object.values(data).map(x => x.name)

clipboard.png


although arrays should be used in this data structure, if you want to traverse such objects, you can use:
var data = {
0: {name: 'asdfadfa'},
1: {name:' asdfadfa'},
2: {name: 'asdfadfa'},
3: {name:' asdfadfa'},
4: {name: 'asdfadfa'},
5: {name:' asdfadfa'}
};

data.length = 6;

Array.prototype.slice.call (data) .map (val = > val.name)


var data = {

    1 : {name: 'qweqweqwe'},
    2 : {name: 'qweqweqwe'},
    3 : {name: 'qweqweqwe'},
    4 : {name: 'qweqweqwe'}
  }

  for(let i in data){
    console.log(data[i].name)
  }
The object.values method of

es6, which is converted directly to an array

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e47d26-43bde.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e47d26-43bde.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?