1

Let's assume I'm obtaining an array of objects from a Node Repository, for example:

results = [
    {
        name: "John",
        surname: "Fool",
        age: 22
    },
    {
        name: "Erik",
        surname: "Owl",
        age: 38
    }
]

How can I filter every object taking only the keys I need, for example avoiding 'age' key?

filteredResults = [
    {
        name: "John",
        surname: "Fool",
    },
    {
        name: "Erik",
        surname: "Owl",
    }
]

I've already obtained this by creating another empty array and populating it by looping on the original one, but in case of large-data this would be heavy.

repository.retrieve((error, result) => {
  let filteredData = [];
  result.forEach(r => {
    filteredData.push({
      name: r.name,
      description: r.description,
    });
  });
});

In SQL, I would obtain it this way:

SELECT `name, description` FROM results;
2
  • I'd probably use a library (e.g., lodash) that has something like a pick function, or just hack together a quick one of your own (it's quite short). Commented Jul 29, 2020 at 16:23
  • Why don't you use MongoDB projections for that? What do you mean by "Node Repository"? Commented Jul 29, 2020 at 17:08

5 Answers 5

2

You can just rebuild the object as you want

  { 
    name: rec.name,
    surname: rec.surname
  }

const results = [
    {
        name: "John",
        surname: "Fool",
        age: 22
    },
    {
        name: "Erik",
        surname: "Owl",
        age: 38
    }
]

const result = results.map((rec) => {
  return { 
    name: rec.name,
    surname: rec.surname
  }
})


console.log(result)

Or delete fields that is useless

const results = [
    {
        name: "John",
        surname: "Fool",
        age: 22
    },
    {
        name: "Erik",
        surname: "Owl",
        age: 38
    }
]

const result = results.map((rec) => {
  delete rec.age
  return rec
})


console.log(result)

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

Comments

1

I suggest you can tell more about what you will need to perform on the output to get the answer that can help.

Case 1. if your original list will survive and you accept your "modified list" to always follow the original list, you may use a generator to wrap your original object, by always not returning those extra properties.

Case 2. if you really want a query system, you may try using real DB thing such as levelDB

Case 3. if you need to display the modified list, write a simple wrapper to fit the format of each list item

Case 4. if you need to snapshot the modified list as object, the method you already made is already a very reasonable method

Case 5. if you need to snapshot the modified list as another output, you can try to directly obtain such output rather than making the intermediate object

Comments

1

You can use map and reduce to simplify this, which obviates the need to create a new array.

var results = [ { name: "John", surname: "Fool", age: 22 }, { name: "Erik", surname: "Owl", age: 38 } ];
let keys = ['name', 'surname'];
var filtered = results.map(obj=>
   keys.reduce((acc,curr)=>(acc[curr]=obj[curr],acc), {}));
console.log(filtered);

You can also use object destructuring.

var results = [ { name: "John", surname: "Fool", age: 22 }, { name: "Erik", surname: "Owl", age: 38 } ];
var filtered = results.map(({name,surname})=>({name,surname}));
console.log(filtered);

Comments

1

Take a look at Array.map.It creates the transformed array.

let arr = [
    {
        name: "John",
        surname: "Fool",
        age: 22
    },
    {
        name: "Erik",
        surname: "Owl",
        age: 38
    }
]

let result = arr.map((elem) => {
  return {
    name: elem.name,
    surname: elem.surname
  }
});

console.log(result);

Comments

0

You can use Array.map() to transform individual elements of the array. And in the callback function use Object destructuring assignment to use only the keys you are interested in and return a new object with only those keys.

let results = [
    { name: "John", surname: "Fool", age: 22 },
    { name: "Erik", surname: "Owl", age: 38 }
];

let modified = results.map(({name, surname}) => ({name, surname}));
console.log(modified);

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.