所以我想按照以下方式过滤我的数组:
让我说(ns simple.strings
(:require [clojure.string :as str]))
(str/starts-with? "hello, world" "hello")
; true
我的数据数组=
array1 = ['c', 'd']
我想过滤掉我的数据数组,以便只有数据数组中的那些对象存在于array1中的userid值。
预期结果:
[{
"userid": "a",
"age": 19,
"name": "john"
}, {
"userid": "c",
"age": 119,
"name": "joy"
}, {
"userid": "d",
"age": 119,
"name": "jesse"
}]
答案 0 :(得分:3)
为了让您更好地理解,添加Tushar的解决方案,而不使用lambda表达式,
newDataArray = dataArray.filter(function(item) {return array1.includes(item.userid)})
在ES5中:
var newDataArray = dataArray.filter(function (item) {
return array1.indexOf(item.userid) !== -1;
});
答案 1 :(得分:0)
var a1 = [
{
"userid": "a",
"age": 19,
"name": "john"
},
{
"userid": "c",
"age": 119,
"name": "joy"
},
{
"userid": "d",
"age": 119,
"name": "jesse"
}
];
var a2 = ['c','d'];
var newObj = [];
a1.filter(function(object,index){
var x = object.userid;
a2.filter(function(userId){
if(x==userId){
newObj[index-1] = object;
}
});
});
console.log(JSON.stringify(newObj));