I want to change the array
transactionProducts: [{
sku: "SKU_12345",
name: "Stan and Friends Tee",
}]
to
items: [{
item_id: "SKU_12345",
item_name: "Stan and Friends Tee",
}]
I tried below:
var items = transactionProducts.map (({sku,name}) =>
({item_id:sku,item_name:name}));
return items;
}
but I soon find the EC6 version is not allowed in my environment, so I have to use older version (meaning cannot use arrows)
How can I do?
You were pretty close
ES5:
const transactionProducts = [
{
sku: 'SKU_12345',
name: 'Stan and Friends Tee',
},
{
sku: 'SKU_12365',
name: 'Stan and Friends Pants',
},
];
const mapped = transactionProducts.map(function (obj) {
return {
item_id: obj.sku,
item_name: obj.name,
};
});
console.log(mapped);
You can use normal function and there is no destructuring in before ES6 so you can directly access properties from object using dot(.) notation
var transactionProducts = [
{
sku: 'SKU_12345',
name: 'Stan and Friends Tee',
},
];
var items = transactionProducts.map(function (obj) {
return { item_id: obj.sku, item_name: obj.name };
});
console.log(items);
This would be ES5 version
var items = transactionProducts.map(function(obj) {
return {
item_id: sku,
item_name: name,
}
})