Skip to content
Advertisement

Show array list as specific list in JQuery/JavaScript

I have this list:

var objData =[{"Id":1,"Title":"T1"},{"Id":3,"Title":"T2"}];

Need to show it on ‘data’, like this:

data{
"1":"T1",
"3":"T2"
}

How could to make a array list and show it on ‘data’, Thanks.

Advertisement

Answer

You could use Array#reduce

const arr =[{"Id":1,"Title":"T1"},{"Id":3,"Title":"T2"}];
const res = arr.reduce((acc,{Id,Title})=>(acc[Id]=Title,acc),{})

console.log(res)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement