admin管理员组文章数量:1397110
Im trying to create a filter function where it can return a result of data that matches the value that i am looking for , from the given set of string keys
example of array:
let data = [
{ id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
{ id:2 , data:{ name:"sample2",address:{ cat:"office" } } },
{ id:3 , data:{ name:"sample3",address:{ cat:"office" } } },
{ id:4 , data:{ name:"sample4",address:{ cat:"office" } } }
{ id:5 , data:{ name:"sample5",address:{ cat:"home" } } }
{ id:6 , data:{ name:"sample6",address:{ cat:"home" } } }
]
function filter( collection , value ,key ){
//code
}
let result = filter( data , "business" , [ "data","address","cat" ] )
console.log(result)
expected result is
{ id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
Im trying to create a filter function where it can return a result of data that matches the value that i am looking for , from the given set of string keys
example of array:
let data = [
{ id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
{ id:2 , data:{ name:"sample2",address:{ cat:"office" } } },
{ id:3 , data:{ name:"sample3",address:{ cat:"office" } } },
{ id:4 , data:{ name:"sample4",address:{ cat:"office" } } }
{ id:5 , data:{ name:"sample5",address:{ cat:"home" } } }
{ id:6 , data:{ name:"sample6",address:{ cat:"home" } } }
]
function filter( collection , value ,key ){
//code
}
let result = filter( data , "business" , [ "data","address","cat" ] )
console.log(result)
expected result is
Share Improve this question asked May 30, 2018 at 3:23 JakegarboJakegarbo 1,30110 silver badges21 bronze badges 3{ id:1 , data:{ name:"sample1",address:{ cat:"business" } } },
- You want to search for any of the of key, right? Mean to say, sometimes key may be id, name, address also? – hygull Commented May 30, 2018 at 3:28
- No the key is the exact address of the value in the array. it could be also string or object you can modify it. – Jakegarbo Commented May 30, 2018 at 3:40
- 1 Very nice question – Isaac Commented May 30, 2018 at 3:55
4 Answers
Reset to default 6You can use filter
to search for the data. Use reduce
to construct the keys.
Note: filter
returns an array of matched elements. If you prefer the first match only, you can use find
const data = [
{ id: 1, data: { name: "sample1", address:{ cat: "business" } } },
{ id: 2, data: { name: "sample2", address:{ cat: "office" } } },
{ id: 3, data: { name: "sample3", address:{ cat: "office" } } },
{ id: 4, data: { name: "sample4", address:{ cat: "office" } } },
{ id: 5, data: { name: "sample5", address:{ cat: "home" } } },
{ id: 6, data: { name: "sample6", address:{ cat: "home" } } }
]
const filter = (collection, keys, value) =>
collection.filter(o => keys.reduce((c, v) => c[v] || {}, o) === value)
const result = filter(data, ["data", "address", "cat"], "business")
console.log(result)
function filter( collection , value ,key ){
const getNestedObjectValue = (nestedObject, propertyPath) => {
return propertyPath.reduce((obj, key) =>
(obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObject);
};
return collection.filter( item => getNestedObjectValue(item, key) === value);
}
The filter function will return an array of matching object(s) when there is a match and an empty array when there is no match
let result = filter( data , "business" , [ "data","address","cat" ] );
console.log(result); // [{"id":1,"data":{"name":"sample1","address":{"cat":"business"}}}]
let result2 = filter( data , "office" , [ "data","address","cat" ] );
console.log(result2); //[{"id":2,"data":{"name":"sample2","address":{"cat":"office"}}},{"id":3,"data":{"name":"sample3","address":{"cat":"office"}}},{"id":4,"data":{"name":"sample4","address":{"cat":"office"}}}]
let result3 = filter( data , "vacation" , [ "data","address","cat" ] );
console.log(result2); // []
You can try below code.
Please ment if it doesn't satisfy your problem or if you want more functionality to be added in the code. I will update my answer.
function filter( collection , value ,key ){
for(var obj of collection) {
if(obj[key[0]][key[1]][key[2]] == value)
{
return obj
}
}
return null;
}
Clean code using Underscore and ES6 arrow notation.
const equalTo = expected => actual => expected === actual;
function filterDeepValue(collection, value, path) {
const criterion = _.pose(equalTo(value), _.property(path));
return _.filter(collection, criterion);
}
const data = [
{id: 1, data: {name: "sample1", address: {cat: "business"}}},
{id: 2, data: {name: "sample2", address: {cat: "office"}}},
{id: 3, data: {name: "sample3", address: {cat: "office"}}},
{id: 4, data: {name: "sample4", address: {cat: "office"}}},
{id: 5, data: {name: "sample5", address: {cat: "home"}}},
{id: 6, data: {name: "sample6", address: {cat: "home"}}},
];
console.log(filterDeepValue(data, 'business', ['data', 'address', 'cat']));
<script src="https://underscorejs/underscore-umd-min.js"></script>
本文标签: javascriptFilter multiple array object with multiple propertyStack Overflow
版权声明:本文标题:javascript - Filter multiple array object with multiple property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106313a2591080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论