0

I receive a json object with a promise call that has the following structure:

[{ key1:'value1', key2:'value2',...},{ key1:'value1', key2:'value2',...},...]

what I wanna do with this is to "filter" some keyx:'valuex' pairs from every object (which could be done with a map function, I suppose).

The best I could accomplish was by doing:

x.response.map((r) => new Map([['number',r.number],['hash', r.hash]]))

But that gives me a [Map(2), Map(2)] object

I want simply a [{...}, {...}] object

Example:

Input:

[{id:1, name:'John', age:30}, {id:2, name:'Mary', age: 24}, {id:3, name:'Jack', age: 32}]

Output:

[{name:'John', age:30}, {name:'Mary', age: 24}, {name:'Jack', age: 32}]

Tadeu Moreira
  • 31
  • 1
  • 8
  • Note that there is an [`Array.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) method. It returns a new array consisting only of elements that satisfy the passed conditional/callback. This in mind, your question could really benefit from a small snippet of actual data, along with the expected result. When you say "I want to filter some pairs", it's not clear if you mean filter them *out*, or filter everything *else* out. It's also not clear on what the filtering logic is supposed to be. – Tyler Roper May 22 '19 at 20:45
  • I don't know if `filter()` apply in this case. I believe he needs to drop some `properties` of the objects that are inside the array. Could you elaborate with a sample of input and expected output? – Shidersz May 22 '19 at 20:49
  • I guess I've used an unaccurate term. My goal is not to impose conditions, but just to really discard some pairs from every object. Let we say I want only k1=>v1 and k2=>v2 from every object. It's kind of analogue to ```SELECT (row1,row2,...)...``` in SQL – Tadeu Moreira May 22 '19 at 20:52
  • Can you provide a sample input value, and the expected output to help illustrate what you're trying to do? – Arash Motamedi May 22 '19 at 20:52
  • Promises? Responses? `.map` function? `Map` objects? Filtering? I have no idea what these have to do with another, or what you want to do. – Bergi May 22 '19 at 20:55
  • I've updated with an input/output example – Tadeu Moreira May 22 '19 at 20:57
  • Dear @TadeuMoreira, there are many questions on Stack Overflow on how to map arrays and transform them into objects of all kinds, I see one question like this every day. I know you probably know SO from a friend but please do take time to read some of https://stackoverflow.com/help, I think it will help you in the long run. Search for "map transform array object keys values" and you will find some information that will help you debug your code problem. Also, welcome to Stack Overflow! – Armen Michaeli May 22 '19 at 21:00
  • As I don't see it as an option in the marked duplicate, I would suggest using `.map()` so that you leave your original array unaltered: `input.map(({name, age}) => ({name: name, age: age}))` – Tyler Roper May 22 '19 at 21:03
  • I would also suggest [**this**](https://stackoverflow.com/questions/44348862/remove-key-from-all-objects-in-array) as more apt duplicate. – Tyler Roper May 22 '19 at 21:04
  • Thanks for the resources, I'll give them all a read. And thanks for the patience because it seems to be a recurring type of question – Tadeu Moreira May 22 '19 at 21:05
  • So, this question is actually solved [here](https://stackoverflow.com/questions/44348862/remove-key-from-all-objects-in-array) instead of the topic to which this was marked as duplicate, because I'd have to spend way too many lines with delete. Don't know if this can be fixed – Tadeu Moreira May 22 '19 at 21:11

0 Answers0