I have the following Array:
const arr = [{
id: 0,
title: 'A',
countries: [{
val: "1173",
label: "England"
}, {
val: "1172",
label: "Egypt"
}],
companies: [{
val: "7346",
label: "Ab Company"
}]
},
{
id: 1,
title: 'B',
countries: [{
val: "1175",
label: "France"
}],
companies: [{
val: "8294",
label: "Cd Company"
}]
},
]
What I want to achieve is:
const arr = [{
id: 0,
title: 'A',
countries: ["England", "Egypt"],
companies: ["Ab Company"]
},
{
id: 1,
title: 'B',
countries: ["France"],
companies: ["Cd Company"]
},
]
My approach:
const mapJobArrValsToString = (arr) => {
if (!(arr && arr.length)) {
return [];
}
const fieldsToAddLabels = ['companies', 'countries'];
const clonedArr = [...arr];
clonedArr.forEach((job) => {
const objKeysList = Object.keys(job).filter((fieldName) => fieldsToAddLabels.includes(fieldName));
objKeysList.forEach((key) => {
// eslint-disable-next-line no-param-reassign
job[key] = job[key].map((el) => el.label);
});
});
return clonedArr;
};
const arr = [{
id: 0,
title: 'A',
countries: [{
val: "1173",
label: "England"
}, {
val: "1172",
label: "Egypt"
}],
companies: [{
val: "7346",
label: "Ab Company"
}]
},
{
id: 1,
title: 'B',
countries: [{
val: "1175",
label: "France"
}],
companies: [{
val: "8294",
label: "Cd Company"
}]
},
];
console.log(mapJobArrValsToString(arr));
What am I doing wrong ?
asked May 13, 2020 at 7:38
Gutelaunetyp
1,8824 gold badges23 silver badges43 bronze badges
-
1your code works fine - and produces the expected result - why do you think it doesn't?Jaromanda X– Jaromanda X2020年05月13日 07:40:50 +00:00Commented May 13, 2020 at 7:40
-
1According to your own snippet, the code already works as expected, as far as I can see?user5734311– user57343112020年05月13日 07:41:37 +00:00Commented May 13, 2020 at 7:41
2 Answers 2
You could build new object and add this object to the copy of the object.
const
array = [{ id: 0, title: 'A', countries: [{ val: "1173", label: "England" }, { val: "1172", label: "Egypt" }], companies: [{ val: "7346", label: "Ab Company" }] }, { id: 1, title: 'B', countries: [{ val: "1175", label: "France" }], companies: [{ val: "8294", label: "Cd Company" }] } ],
keys = ['countries', 'companies'],
result = array.map(o => ({
... o,
...Object.fromEntries(keys.map(k => [k, o[k].map(({ label }) => label)]))
}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered May 13, 2020 at 7:44
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can do it this way too:
var arr = [{ id: 0, title: 'A', countries: [{ val: "1173", label: "England" }, { val: "1172", label: "Egypt" }], companies: [{ val: "7346", label: "Ab Company" }] }, { id: 1, title: 'B', countries: [{ val: "1175", label: "France" }], companies: [{ val: "8294", label: "Cd Company" }] }];
var keysFilter=['countries', 'companies'];
var result = arr.map((elem) => {
for (const [key, value] of Object.entries(elem)) {
if(keysFilter.includes(key)) elem[key] = value.map(val => val.label);
}
return elem;
});
console.log(result);
I hope this helps. Thanks! Happy Coding!
answered May 13, 2020 at 12:49
Rajneesh
5,3181 gold badge10 silver badges20 bronze badges
Comments
lang-js