Conversion between json and array

{other:1} = > [{name: "other", value:1}]
now if you want to achieve the above transformation, ask for the great guidance

.
Mar.13,2022

var a = {other: 1};

var b = Object.keys(a).map(function(item) {
    return {
        name: item,
        value: a[item]
    }
})

console.log(b)

const obj = {other:1};

const array = [];
Object.keys(obj).forEach((item, idx) => {
    array.push([{
        name: item,
        value: Object.values(obj)[idx]
    }]);
});

console.log(array);

first of all, this thing is not called JSON , JSON is an acronym for JavaScript Object Notation, and {key: value} is a normal object ( object ). You will ask this question to show that you are not familiar with js, so I guess your requirement is probably not necessary, you might as well explain why you want to do this transformation, want to traverse all the key-value pairs of an object can use for-in traversal. Going back to the question itself, you can write this in the latest syntax:

let obj = { other: 1 }

let entries = Object.keys(obj).map(key => { name: key, value: obj[key] })

in fact, there is a Object.entries method in the new proposal, which can be used in the

const obj = {other:1};

let newObj = Object.keys(obj).reduce((arr,item)=>{
     return (arr.push({
         name: item,
         value: obj[item]
     }),arr)
},[])

console.log(newObj)
Menu