Js arrays and objects convert to each other

Object to array

let arr = []
 
let obj = {
    name: "Big East".label: "Little Nuggets"
}
 
for(let key in obj) {
    arr.push({
        name: key,
        label: obj[key]
    })
}
 
console.log('arrStringify'.JSON.stringify(arr, null.2));
console.log('arr', arr);
Copy the code

rendering

Array to object

let arr = [
  {
    "name": "name"."label": "Big East"
  },
  {
    "name": "label"."label": "Little Nuggets"}]let obj = {}
 

arr.forEach(item= > {
    obj[item.name] = item.label
})
 
console.log('obj'.JSON.stringify(obj, null.2));
console.log('obj', obj);
Copy the code

rendering