Home > Enterprise >  Show array list as specific list in JQuery/JavaScript
Show array list as specific list in JQuery/JavaScript

Time:01-01

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.

CodePudding user response:

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)

CodePudding user response:

var objData =[{"Id":1,"Title":"T1"},{"Id":3,"Title":"T2"}];
var data = {};
objData.forEach(o => data[o.Id] = data.Title)
console.log(data);
  • Related