0

i want to retrieve certain properties from the array of object and store it in a array.

the data structure looks like below,

object_arr [{…}]0: first_property: "color" id: 25 x: 10 y: 50 z: 56 _proto__: Objectlength: 1__proto__: Array(0) 

i want to retrieve x, y and z values so the output should be like below

(3) [10, 50, 56] 0: 10 1: 50 2: 56 length: 3 __proto__: Array(0) so final_output[0] should give me 10, final_output[1] should give me 50 and final_output[2] should give 56 

what i have tried?

let final_output = object_arr.map(m => { return [ m.x, m.y, m.z ] }); 

the final_output looks like this,

final_output [Array(3)] 0: (3) [10, 50, 56] length: 1 the expected output is as below, final_output (3) [10, 50, 56] 0: 10 1: 50 2: 56 length: 3 

how can i fix this. could someone help me with this. thanks.

6
  • 1
    What's the issue with final_output = final_output[0]? Commented Nov 5, 2019 at 13:12
  • What is your expected output? Commented Nov 5, 2019 at 13:14
  • i have updated the question what final output looks like with the code that i had tried. Commented Nov 5, 2019 at 13:15
  • @someuser2491 can you show the expected output when you have multiple objects in your input array (instead of just one)? Commented Nov 5, 2019 at 13:16
  • 1
    I'll just answer the literal question, although i am quite confused about this one: finalOutput = (({x, y, z}) => [x, y, z])(object_arr[0]). Commented Nov 5, 2019 at 13:20

4 Answers 4

1

Do this:

let object_arr = { first_property: "color", id: 25, x: 10, y: 50, z: 56} let final_output = [object_arr.x, object_arr.y, object_arr.z]; 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks this should work. can this be done in single statement.
I don't think that's the input format. There's an array wrapped around it.
1

This is quite simple. I think your confusion is stemming from trying to work with the input array as a whole. If you only want the data from its first element, you don't need map. Just address it directly with [0]:

let object_arr = [{ first_property: "color", id: 25, x: 10, y: 50, z: 56}] let final_output = [object_arr [0] .x, object_arr [0] .z, object_arr [0] .z]; console .log (final_output)

Comments

0

If your array only contains the one object, you can add its properties directly into an array like so:

const object_arr = [{first_property:"color",id:25,x:10,y:50,z:56}]; const [{x, y, z}] = object_arr; // use destructuring to get the object keys from the array const res = [x, y, z]; // create an array containing x, y, z properties console.log(res);

If your array can contain multiple objects, you could use .flatMap() by mapping your properties to an array, which will then fall into a larger resulting array. This way, all x, y and z properties (of all objects) will be mapped to one larger output array:

const object_arr = [{first_property:"color",id:25,x:10,y:50,z:56}]; const res = object_arr.flatMap(o => [o.x, o.y, o.z]); console.log(res);

2 Comments

The flatMap version would be odd in the case where there is more than one element in the input array. It's possibly what would be wanted, with a sequence of x, y, and z values, but that seems unlikely.
@ScottSauyet I agree. I originally thought it seemed odd and would've imagined that the solution provided earlier today was enough to fulfil OPs needs. However, it seems as though it wasn't what the OP was after, so I assumed they wanted a flatMap if they're using an array. Overall both questions aren't very clear in my eyes, and so I gave my best estimate at what the OP was after (hence the 2 solutions)
0

You can clearly see : final_output [Array(3)] 0: (3) [10, 50, 56] which is array of array. So to get value 10 you will have to do: final_output[0][0]. Or output=final_output[0]; Then output[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.