6

My code has an array of elements as follows:

element: { fromX: { id: ... } , toX: { id: ... } }

Requirement is to pull all the fromX ids into one array, and all toX ids into other.

There are a couple of different ways, such as using foreach, reduce, iterating for each respectively, but I'm searching for an optimal functional way to return two arrays with one mapping?

5
  • do you have only two properties in an object? Commented Jan 15, 2019 at 14:57
  • 1
    Can you provide a more thorough input and output? Thanks. Commented Jan 15, 2019 at 14:57
  • What do you expect to get? An array that contains two arrays with the mapped values? Commented Jan 15, 2019 at 14:57
  • Can you expand the example code a bit more to show the full structure? I.e., what does it look like when multiple fromX and fromY values are shown? Commented Jan 15, 2019 at 14:57
  • 1
    [froms, tos] = transpose(elements.map(e => [e.fromX, e.toX])) is the functional way - unfortunately there is no native transpose function in JS Commented Jan 15, 2019 at 15:04

5 Answers 5

11

Using Array#reduce and destructuring

const data=[{fromX:{id:1},toX:{id:2}},{fromX:{id:3},toX:{id:4}},{fromX:{id:5},toX:{id:6}},{fromX:{id:7},toX:{id:8}}]

const [fromX,toX] = data.reduce(([a,b], {fromX,toX})=>{
  a.push(fromX.id);
  b.push(toX.id);
  return [a,b];
}, [[],[]]);

console.log(fromX);
console.log(toX);

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

1 Comment

Very very elegant!
2

You could take an array for the wanted keys and map the value. Later take a destructuring assignment for getting single id.

const
    transpose = array => array.reduce((r, a) => a.map((v, i) => [...(r[i] || []), v]), []),
    array = [{ fromX: { id: 1 }, toX: { id: 2 } }, { fromX: { id: 3 }, toX: { id: 4 } }],
    keys = ['fromX', 'toX'],
    [fromX, toX] = transpose(array.map(o => keys.map(k => o[k].id)));

console.log(fromX);
console.log(toX);

3 Comments

I believe your answer is not respecting one requirements of the question "I would like to pull all the fromX ids into one array, and all toX ids into other.".
@thibpat, please see fromX and toX.
When running your snippet, I get: fromX = [1,2] and toX = [3,4]. I believe the expected result is fromX = [1, 3] and toX = [2, 4].
0

Try this:

const arr = [{
  fromX: {
    id: 1
  },
  toX: {
    id: 2
  }
}, {
  fromX: {
    id: 3
  },
  toX: {
    id: 4
  }
}]
let {
  arr1,
  arr2
} = arr.reduce((acc, {
  fromX,
  toX
}) => ({ ...acc,
  arr1: [...acc.arr1, fromX],
  arr2: [...acc.arr2, toX]
}), {
  arr1: [],
  arr2: []
})
console.log(arr1, arr2);

Comments

0

You can achieve this by using below solution

var array = [{ fromX: { id: 1 }, toX: { id: 2 } }, { fromX: { id: 3 }, toX: { id: 4 } }], 
 arrayX = array.map(x => x.fromX), arraytoX = array.map(toX => toX.toX)

console.log(arrayX);
console.log(arraytoX );

Comments

0

Returning multiple keys from the list requires using maps in jQuery.

var list = [{ name:"h1",emailId:"[email protected]",password:"h1"},{ name:"h2",emailId:"[email protected]",password:"h2"},name:"h3",emailId:"[email protected]",password:"h3"}]

Obtain only the name and emailId from this list. Subsequently, use the map keyword.

var filter = list.map(x => ({name : x.name, email : x.emailId }));

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.