items = [
{
id: "1",
name: "тест1",
parents_names: ["Московская", "Орехово-Зуевский"],
parents_levels: [1, 3]
},
{
id: "2",
name: "Тест2",
parents_names: ["Карелия", "Прионежский", "Центральный"],
parents_levels: [1, 3, 7]
},
{
id: "3",
name: "Тест3",
parents_names: ["Санкт-Питер"],
parents_levels: [1]
},
];
newItems = [
{
id: "1",
name: "тест1",
region: "Московская",
area: "Орехово-Зуевский"
},
{
id: "2",
name: "тест2",
region: "Московская",
area: "Орехово-Зуевский",
street: "Центральный"
},
{
id: "3",
name: "тест3",
region: "Санкт-Питер",
}
];
const TYPES = {
1: 'region',
3: 'area',
7: 'street',
};
const newItems = items.map(({ parents_names, parents_levels, ...n }) =>
parents_names.reduce((acc, name, i) => (
acc[TYPES[parents_levels[i]]] = name,
acc
), n)
);
var items = [
{
id: "1",
name: "тест1",
parents_names: ["Московская", "Орехово-Зуевский"],
parents_levels: [1, 3]
},
{
id: "2",
name: "Тест2",
parents_names: ["Карелия", "Прионежский", "Центральный"],
parents_levels: [1, 3, 7]
},
{
id: "3",
name: "Тест3",
parents_names: ["Санкт-Питер"],
parents_levels: [1]
},
];
var data = {
1 : 'region',
3 : 'area',
7 : 'street'
}
console.log(items.map(function(i) {
i.parents_levels.forEach(function(p, ind) {
i[data[p]] = i.parents_names[ind];
})
delete i.parents_names;
delete i.parents_levels;
return i;
}));