对不同集合

时间:2018-03-28 07:13:17

标签: javascript arrays sorting lodash

我有一个对象数组

array = [
    {id: 5, name: "Helen", age: 20}, 
    {id: 15, name: "Lucy", age: 30}, 
    {id:7, name: "Carlos", age: 1}
]

然后我有一个类似的数组排序

arraySorted = [        
    {id: 15, name: "Lucy", age: 2}, 
    {id: 5, name: "Lara", age: 11}, 
    {id:7, name: "Carlos", age: 10}
]

两个数组上的对象的id总是匹配,其余的属性可能会也可能不会。

我需要的是以与id相同的arraySorted顺序对数组进行排序。

(也可以在普通JavaScript上完成,lodash不是必需的,但可能会有用)

6 个答案:

答案 0 :(得分:3)

有关详细信息,请参阅MapArray.prototype.map()



// Raw.
const raw = [
    {id: 5, name: "Helen", age: 20}, 
    {id: 15, name: "Lucy", age: 30}, 
    {id:7, name: "Carlos", age: 1}
]

// Sorted.
const sorted = [        
    {id: 15, name: "Lucy", age: 2}, 
    {id: 5, name: "Lara", age: 11}, 
    {id:7, name: "Carlos", age: 10}
]

// Match.
const match = (raw, sorted) => (m => sorted.map(s => m.get(s.id)))(new Map(raw.map(r => [r.id, r])))

// Output.
const output = match(raw, sorted)

// Proof.
console.log(output)




答案 1 :(得分:1)

而不是sort,而不是使用mapfindObject.assign

arraySorted.map( s => Object.assign( s, array.find( t => t.id == s.id ) ) );

<强>演示

&#13;
&#13;
var array = [{
    id: 5,
    name: "Helen",
    age: 20
  },
  {
    id: 15,
    name: "Lucy",
    age: 30
  },
  {
    id: 7,
    name: "Carlos",
    age: 1
  }
];
var arraySorted = [{
    id: 15,
    name: "Lucy",
    age: 2
  },
  {
    id: 5,
    name: "Lara",
    age: 11
  },
  {
    id: 7,
    name: "Carlos",
    age: 10
  }
];
array = arraySorted.map(s => Object.assign(s, array.find(t => t.id == s.id)));

console.log(array);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以使用Map对数组进行排序:

let a1 = [{id: 5, name: "Helen", age: 20}, {id: 15, name: "Lucy", age: 30}, {id:7, name: "Carlos", age: 1}],
    a2 = [{id: 15, name: "Lucy", age: 2}, {id: 5, name: "Lara", age: 11}, {id:7, name: "Carlos", age: 10}];

let map = ((m) => (
             a2.forEach(({id}) => m.set(id, a1.find(o => o.id === id))), m
          ))(new Map());

let result = [...map.values()];

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:1)

可以通过Array.prototype.map获取新数组:

const newArray = arraySorted.map(sortedItem => 
  array.find(item => item.id === sortedItem.id)
)

console.log(newArray)

答案 4 :(得分:1)

您可以使用Map,避免使用嵌套迭代方法(如find,这会使解决方案的时间复杂度更差),假设您确实拥有所有相同的ID两个数组中的值:

&#13;
&#13;
const array = [{id: 5, name: "Helen", age: 20}, {id: 15, name: "Lucy", age: 30}, {id:7, name: "Carlos", age: 1}],
    arraySorted = [{id: 15, name: "Lucy", age: 2},{id: 5, name: "Lara", age: 11},{id:7, name: "Carlos", age: 10}]

const result = arraySorted.map((map => row => array[map.get(row.id)])
                               (new Map(array.map((row, i) => [row.id, i]))));

console.log(result);
&#13;
&#13;
&#13;

答案 5 :(得分:1)

试试这个,希望这会有所帮助

var array=[{id:5,name:"Helen",age:20},{id:15,name:"Lucy",age:30},{id:7,name:"Carlos",age:1}];

var arraySorted=[{id:15,name:"Lucy",age:2},{id:5,name:"Lara",age:11},{id:7,name:"Carlos",age:10}];

array = arraySorted.map(s => array.find(t => t.id == s.id));

console.log(array);