简体   繁体   English

使用一个键的值数组过滤许多javascript对象

[英]Filter a number of javascript objects using an array of values for one key

I want to filter the following objects by category_id using the following array of category_id values: 我想使用以下category_id值数组按category_id过滤以下对象:

[19012000, 19012001, 19012002, 19012003, 19012004, 19012005, 19012006, 19012007, 19012008] 



Object {_account: "mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz", _id: "rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61", amount: 15, category: Array[2], category_id: "13005015"…}
Object {_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj", _id: "944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB", amount: 50, category: Array[3], category_id: "19012000"…}
Object {_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj", _id: "rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK", amount: 24, category: Array[2], category_id: "13005007"…}
Object {_account: "mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz", _id: "rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61", amount: 45, category: Array[2], category_id: "13005009"…}
Object {_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj", _id: "944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB", amount: 105, category: Array[3], category_id: "13005009"…}

When finished the object with category_id 19012000 should be the only one left. 完成后,类别ID为19012000的对象应该是剩下的唯一对象。

Object {_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj", _id: "944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB", amount: 50, category: Array[3], category_id: "19012000"…}

I've made a number of attempts with different versions of iteratee functions with filter, map and each and tried key value filters with keys, values, invert, pick and omit. 我已经尝试过使用带有过滤器,映射的iteratee函数的不同版本,并尝试使用键,值,取反,选择和忽略键值过滤器。 After trying this, which I believe should work I reach out to you: 尝试了一下之后,我认为应该可以奏效,然后与您联系:

var postDate = _.filter(_.first(goodDates), function(n){return category_id.indexOf(n.category_id) !== -1})

where goodDates is the list of objects and category_id is the array above. 其中goodDates是对象列表,category_id是上面的数组。

indexOf() with a string and a number 具有字符串和数字的indexOf()

var a = [123]; 
console.log(a.indexOf(123));  //true   
console.log(a.indexOf("123"));  //false

Your code is not working because you are comparing a string in your object to the numbers in your array. 您的代码无法正常工作,因为您正在将对象中的字符串与数组中的数字进行比较。

category_id.indexOf(+n.category_id)

or 要么

category_id.indexOf(parseInt(n.category_id, 10))

I don't see any reason to use _.first here? 我看不出有任何理由在这里使用_.first吗? The rest of your filter statement looks good, altough you may swap the indexOf call for _.contains : 其余的filter语句看起来不错,尽管您可以将indexOf调用_.contains

var postDates = _.filter(goodDates, function(n){
    return _.contains(category_id, n.category_id);
});

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

相关问题 从一个 Array 对象中提取键值,并使用带下划线 javascript 的提取值从其他对象中过滤 - Extract key values from one Array object and filter from other objects with the extracted values with underscore javascript 从值数组 JavaScript 中过滤键上的对象数组 - Filter array of objects on key from array of values JavaScript 如何将 Javascript 对象数组过滤为一列的不同值 - How to filter Javascript array of Objects to distinct values of one Column 根据对象键过滤对象数组 JavaScript - Filter JavaScript Array of Objects based on Objects Key 使用所需键值列表过滤对象数组 - Filter array of objects using a list of desired key values JavaScript 使用另一个数组过滤一个对象数组 - JavaScript filter one array of objects using another array Javascript中过滤多个数组对象,匹配键值与多个值形成另一个数组 - Filter multiple Array Objects and match key values with multiple values form another array in Javascript javascript:使用函数式方法通过带有数组的多个值过滤对象 - javascript: filter objects by multiple values with array using a functional approach 使用 javascript 或 lodash 过滤具有多个元素及其值的对象数组 - Filter an array of objects with multiple elements with their values using javascript or lodash 按 JavaScript 中的键和嵌套值过滤对象数组 - Filter an array of objects by key and nested value in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM