简体   繁体   English

如何按照包含每个对象中存在的属性的唯一值的另一个数组的顺序对对象数组进行排序?

[英]How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

I have have two arrays, I want to merge them into one object. 我有两个数组,我想将它们合并为一个对象。 I have given some examples of what I have and what I want to achieve. 我已经举例说明了我所拥有和想要实现的目标。 I tried _.union and few other underscore methods. 我尝试了_.union和其他一些下划线方法。

var original = [
  {
    Country: 'US',
    value: '10'
  },
  {
    Country: 'Turkey',
    value: '5'
  }
];

var newlist =["Afghanistan", "Antarctica","Turkey"]

The Results I want: 我想要的结果:

var results= [
  {
    Country: 'Afghanistan',
    value: '0'
  },
  {
    Country: 'Antarctica',
    value: '0'
  },
  {
    Country: 'Turkey',
    value: '5'
  }
];

The US would not appear in the final results because the newlist doesn't have US. 美国将不会出现在最终结果中,因为新名单中没有美国。 So basically all the values from the new list would appear in the results with the values from the original list. 因此,基本上新列表中的所有值都会与原始列表中的值一起出现在结果中。

A non-Underscore solution, that .map() s the new array, returning the object from the original array if it can .find() it, otherwise returning a new object: 一个非下划线的解决方案, .map()是新数组,如果可以的话,从原始数组中返回该对象,如果它可以对.find()返回,否则返回新对象:

 var original = [ { Country: 'US', value: '10' }, { Country: 'Turkey', value: '5' } ]; var newlist =["Afghanistan", "Antarctica","Turkey"] var result = newlist.map(function(v) { return original.find(function(o) { return o.Country === v }) || { Country: v, value: '0' } }) console.log(result) 

It's a one-liner with ES6 arrow functions: 它是具有ES6箭头功能的单线:

 var original = [ { Country: 'US', value: '10' }, { Country: 'Turkey', value: '5' } ]; var newlist =["Afghanistan", "Antarctica","Turkey"] var result = newlist.map(v => original.find(o => o.Country===v) || {Country:v, value:'0'}) console.log(result) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Lodash:如何从对象数组中获取唯一值,然后按这些唯一值对这些对象进行排序? - Lodash: How to get unique values from an object array and then sort those objects by those unique values? 如何根据对象的属性对对象数组进行排序 - How to sort array of objects according to a property of the object 对象数组,某些对象包含另一个数组,需要通过包含数组的对象中的值对其进行过滤 - Array of objects with some objects containing another array, need to filter it by values within the object containing an array 按对象中的属性对对象数组进行排序 - sort array of objects by property in object JavaScript:通过每个对象中的数字属性对对象数组进行排序 - JavaScript: Sort an array of objects by a numeric property in each object 如何以唯一的顺序对数组进行排序 - How to sort an array in a unique order 如果元素在另一个对象数组中,则删除包含对象数组内部数组的对象属性 - remove object property containing array inside array of objects if element in another array of objects 如何将数组中存在的每个 object 的值与另一个数组的值相乘? - how to multiply value of each object present in an array with value of another array? 为对象数组中的某些属性返回唯一值的函数,按顺序 - Function that returns unique values for certain property in object array, in order 如何将仅包含数组值的 object 转换为对象数组,每个对象都具有转置属性值? - How to convert an object of just array values into an array of objects, each with transposed property values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM