2

I want to filter an array with data objects with it's keys based on another array.

I need to show/hide columns of a table, which is based on data like this:

var data = [{
    id: "1",
    name: "A",
    description: "number 1"
  },
  {
    id: "2",
    name: "B",
    description: "number 2"
  }
]

This results in a table with three columns ("ID", "Name" and "Description") and two rows.

I now have a dropdown which contains one checkbox for each table header.

These inputs get stored in another array, so when you click "id" in the dropdown, the array looks like this:

var selectedColumns = ["id"]

..or when you select "id" and "name":

var selectedColumns = ["id", "name"]

I now want to filter my data array based on the selectedColumns array.

I already tried the filter function, but I can't wrap my head around on how to do it right:

   if (selectedColumns) {
    if (selectedColumns.length != 0) {
      data = data.filter(function(item) {
        var keys = Object.keys(item)
        keys.forEach(function(key) {
          if (!selectedColumns.includes(key)) {
            delete item[key];
          }
        })

        return true
      })
    }

Background: This table should be a reusable Vue component, so all the keys ("id", "name", ..) and the table headers change accordingly, which prevents hardcoding the logic.

5
  • Do you mean you want the elements of data to have only the keys defined in selectedColumns? Commented Jun 19, 2018 at 16:01
  • 1
    ...I now want to filter my data array based on the selectedColumns array... Does this means you want to hide the columns which are not in the selectedColumns arrays? Commented Jun 19, 2018 at 16:01
  • @AhmedFasih Exactly. Commented Jun 19, 2018 at 16:02
  • @lealceldeiro Yes. Commented Jun 19, 2018 at 16:02
  • That's not a filter, that's a map, like Nina's answer specifies. Commented Jun 19, 2018 at 16:03

2 Answers 2

4

You could take the wanted keys and map a new array with new objects with only the wanted properties.

function getProperties(array, keys) {
    return array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));
}

var data = [{ id: "1", name: "A", description: "number 1" }, { id: "2", name: "B", description: "number 2" }];

console.log(getProperties(data, ['id', 'name']));
.as-console-wrapper { max-height: 100% !important; top: 0; }

An even shorter approach with Object.fromEntries.

function getProperties(array, keys) {
    return array.map(o => Object.fromEntries(keys.map(k => [k, o[k]])));
}

var data = [{ id: "1", name: "A", description: "number 1" }, { id: "2", name: "B", description: "number 2" }];

console.log(getProperties(data, ['id', 'name']));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

Try this and it works:

var data = [{
    id: "1",
    name: "A",
    description: "number 1"
  },
  {
    id: "2",
    name: "B",
    description: "number 2"
  }
];

var selectedColumns = ["id", "name"];


if (selectedColumns) {
    if (selectedColumns.length != 0) {
      var data = data.filter(function(item) {
        var keys = Object.keys(item)
        keys.forEach(function(key) {
          if (!selectedColumns.includes(key)) {
            delete item[key];
          }
        })

        return true
      })
    }
    console.log(data);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.