Reputation: 3496
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?
Upvotes: 0
Views: 1576
Reputation: 5815
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[obj.code] =item;
return obj;
}, {});
Upvotes: 1
Reputation: 122888
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>
Upvotes: 0
Reputation: 49945
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);
Upvotes: 1
Reputation: 14462
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);
Upvotes: 0
Reputation: 370639
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);
Upvotes: 3
Reputation: 50291
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)
Upvotes: 0