12

I have an array as follows:

items = [
{"year": 2010, "month": 11, "day":23}
{"year": 2009, "month": 10, "day":15}
{"year": 2009, "month": 10, "day":10} //added after my edit below
]

I would like to create a new array with only 2 of the columns like this:

newArarry = [
{"year": 2010, "month": 11}
{"year": 2009, "month": 10}
]

Right now Im trying with .map() and its not working:

const newArray = [];
newArray.map({
    year: items.year,
    month: items.month
});

EDIT

After following one of the answers below, I realized I forgot to mention that I would also need to filter the result to be only unique rows. Now that I am only selecting the year and month columns, I am getting multiple duplicate rows

Mennyg
  • 330
  • 2
  • 11

6 Answers6

7

Right now Im trying with .map() and its not working

  • Because newArray is having length 0 and you're trying to map on it
  • map accepts a callback but you're passing object
  • Not assigning output of map to any variable

let items = [{"year": 2010, "month": 11, "day":23},{"year": 2009, "month": 10, "day":15}]

let final = items.map(({day,...rest}) => ({...rest}))

console.log(final)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
6

You are not calling Array#map correctly:

  1. You need to call in on the source array: items.
  2. You need to provide a function as an argument.
  3. Array#map returns a new array that is the result of the function being executed against each member of the original array.

items = [
{"year": 2010, "month": 11, "day":23},
{"year": 2009, "month": 10, "day":15}
]

const newArray = items.map(item => ({
    year: item.year,
    month: item.month
}));

console.log(newArray)

A mapping operation transforms from A to B - in this case from {"year": 2010, "month": 11, "day":23} to { "year": 2010, "month": 11 }. Running a .map on an array gives you the result of the mapping operation.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Thanks, I am currently using this method. However, I am now getting duplicate rows. Is there a way to only select unique rows (my items array is much larger than whats in my question) – Mennyg May 21 '19 at 08:53
  • @Mennyg have a look [at this answer](https://stackoverflow.com/a/9229821/3689450) – VLAZ May 21 '19 at 08:56
5

Array's .map() method accepts a callback whose returned value will be used to construct new array. Mistakenly you are passing it an object instead of a function. To fix this you can use .map() with some Object Destructuring:

const items = [
  {"year": 2010, "month": 11, "day":23},
  {"year": 2009, "month": 10, "day":15}
];

const result = items.map(({ year, month }) => ({year, month}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
3

Array.map() expects a callback as the first parameter, it must also be called on the original array - otherwise you're essentially iterating over an empty array.

var newArray = items.map(function (item) {
    return {
        year: item.year,
        month: item.month
    };
});

Update

In order to make sure your array only contains unique values, rather than mapping and filtering, you could do something similar to the following:

var items = [
    {year: 2010, month: 11, day: 23},
    {year: 2009, month: 10, day: 15},
    {year: 2009, month: 10, day: 10}
];

var newArray = [];

items.forEach(function (item) {
    var index = newArray.findIndex(function (newItem) {
        return newItem.year === item.year && newItem.month === item.month;
    });

    if (index === -1) {
        newArray.push({ year: item.year, month: item.month });
    }
});

console.log(newArray); // [{ year: 2010, month: 11 }, { year: 2009, month: 10 }]
Jack
  • 737
  • 5
  • 10
2

You're not using map correctly - you can also simplify with rest, spread, and destructuring:

const items = [{"year": 2010, "month": 11, "day":23},{"year": 2009, "month": 10, "day":15}];
const newArray = items.map(({ day, ...res }) => ({ ...res }));
console.log(newArray);
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

Correct your items array, Array with multiple elements/values must be comma separated

var items = [{"year": 2010, "month": 11, "day":23},
               {"year": 2009, "month": 10, "day":15}]
                    const newArray = [];
                    items.map(i=>{
                    newArray.push({year: i.year, month: i.month});
                    });
                  console.log(newArray);
Dilip Chauhan
  • 147
  • 2
  • 10