Attempting to learn javascript. I am trying to create Table Row data from data in state (being sent to components via Context API):
myArr = [
{id: 1, name: AA, desc: DescA, color: red},
{id: 2, name: BB, desc: DescB, color: orange},
{id: 3, name: CC, desc: DescC, color: green},
{id: 4, name: DD, desc: DescD, color: blue},
]
I would like to pull only name and color from each object, and create a new arr of objects.
newArr = [
{name: AA, color: red}
{name: BB, color: orange}
{name: CC, color: green}
{name: DD, color: blue}
]
I would like to do this to match my table data to table headers:
const headCells = [
{ id: 'name', numeric: false, disablePadding: true, label: 'Name' },
{ id: 'color', numeric: false, disablePadding: false, label: 'Color' },
];
I have tried .map and Array.prototype.forEach(), but I am using them incorrectly:
var newArray = activities.map(function(item) {return item["name", "color"]; })
Apologies for this almost certainly being a duplicate. Any help would be really appreciated. Thanks!
desctocolor?.mapis the right approach, your syntax is just slightly wrong.return { name: item.name, color: item.color }