This is my initial array:
[
{
id: 1,
name: 'pos',
code: 'pos123',
appCode: 22,
},
{
id: 2,
name: 'tailor',
code: 'tailor123',
appCode: 21,
}
]
My desired output:
{
pos123: {
id: 1,
name: 'pos',
code: 'pos123',
appCode: 22,
},
tailor123: {
id: 2,
name: 'tailor',
code: 'tailor123',
appCode: 21,
},
}
I tried doing this with map
, but I couldn't figure it out. Should I be using reduce
here?
Your output is not an array - it's an object, so you have to use reduce
. You can only use map
to turn X number of items in an array into X number of items in another array.
const input=[{id:1,name:'pos',code:'pos123',appCode:22,},{id:2,name:'tailor',code:'tailor123',appCode:21,}] const output = input.reduce((a, obj) => { a[obj.code] = obj; return a; }, {}); console.log(output);
You can use Object.assign
mapping your array to expected shape of keys and values. Try:
let array = [ { id: 1, name: 'pos', code: 'pos123', appCode: 22, }, { id: 2, name: 'tailor', code: 'tailor123', appCode: 21, } ]; let output = Object.assign(...array.map(x => ({ [x.code]: x }))); console.log(output);
var yourArray = [
{
id: 1,
name: 'pos',
code: 'pos123',
appCode: 22,
},
{
id: 2,
name: 'tailor',
code: 'tailor123',
appCode: 21,
}
];
//this is what you required
var output = yourArray.reduce((obj, item) => {
obj[code] =item;
return obj;
}, {});
Use array reduce function
var data = [{ id: 1, name: 'pos', code: 'pos123', appCode: 22, }, { id: 2, name: 'tailor', code: 'tailor123', appCode: 21, } ] var modData = data.reduce(function(acc, curr) { //acc is the empty array passed as argument // if empty array does not have a property pos123 or so // create a key by this name if (!acc.hasOwnProperty(curr.code)) { // then add property to it acc[curr.code] = { id: curr.id, name: curr.name, appCode: curr.appCode } } return acc; }, {}) console.log(modData)
Using loop you can do this.
const resp = [ { id: 1, name: 'pos', code: 'pos123', appCode: 22, }, { id: 2, name: 'tailor', code: 'tailor123', appCode: 21, } ] let res = {}; for (let i = 0; i < resp.length; i++) { res[resp[i]['code']] = { id: resp[i]['id'], name: resp[i]['name'], appCode: resp[i]['appCode'], code: resp[i]['code'] } } console.log(res);
Seems the other solutions include 'code'-property in the resulting object. This one doesn't
const test = [ { id: 1, name: 'pos', code: 'pos123', appCode: 22, }, { id: 2, name: 'tailor', code: 'tailor123', appCode: 21, } ]; const test2 = JSON.parse(JSON.stringify(test)); const result = document.querySelector("#result"); result.textContent = "**No redundancy\\n" + JSON.stringify( test.reduce( (newObj, el) => (newObj[el.code] = delete el.code && el) && newObj, {} ), null, " "); // alternative result.textContent += "\\n\\n**Alternative\\n" + JSON.stringify( test2.reduce( (newObj, el) => (newObj[el.code] = el) && newObj, {} ), null, " ");
<pre id="result"></pre>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.