最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return multiple arrays using "map" function - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jan 15, 2019 at 15:41 Muhammad Faizan Uddin 1,36713 silver badges29 bronze badges asked Jan 15, 2019 at 14:52 800c25d6-cd74-11ed-afa1-0242ac800c25d6-cd74-11ed-afa1-0242ac 4291 gold badge7 silver badges14 bronze badges 5
  • do you have only two properties in an object? – Nina Scholz Commented Jan 15, 2019 at 14:57
  • 1 Can you provide a more thorough input and output? Thanks. – kockburn Commented Jan 15, 2019 at 14:57
  • What do you expect to get? An array that contains two arrays with the mapped values? – VLAZ 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? – Nick 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 – Bergi Commented Jan 15, 2019 at 15:04
Add a comment  | 

5 Answers 5

Reset to default 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);

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);

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);

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 );

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 }));
发布评论

评论列表(0)

  1. 暂无评论