I have a multidimensional object (it's basically an array):
我有一个多维对象(它基本上是一个数组):
Object = {
1 : { name : bob , dinner : pizza },
2 : { name : john , dinner : sushi },
3 : { name : larry, dinner : hummus }
}
I want to be able to search the object/array for where the key is "dinner", and see if it matches "sushi".
我希望能够搜索对象/数组,找到键是“dinner”的位置,并查看它是否与“sushi”匹配。
I know jQuery has $.inArray, but it doesn't seem to work on multidimensional arrays. Or maybe I'm wrong. indexOf also seems to only work on one array level.
我知道jQuery有$。inArray,但它在多维数组中不起作用。也许我错了。indexOf似乎只在一个数组级别上工作。
Is there no function or existing code for this?
这里没有函数或现有代码吗?
7 个解决方案
#1
168
If you have an array such as
如果你有一个数组,比如
var people = [
{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
];
You can use the filter
method of an Array object:
可以使用数组对象的筛选方法:
people.filter(function (person) { return person.dinner == "sushi" });
// => [{ "name": "john", "dinner": "sushi" }]
In newer JavaScript implementations you can use a function expression:
在较新的JavaScript实现中,可以使用函数表达式:
people.filter(p => p.dinner == "sushi")
// => [{ "name": "john", "dinner": "sushi" }]
You can search for people who have "dinner": "sushi"
using a map
你可以用地图搜索吃“晚餐”的人:“寿司”
people.map(function (person) {
if (person.dinner == "sushi") {
return person
} else {
return null
}
}); // => [null, { "name": "john", "dinner": "sushi" }, null]
or a reduce
或减少
people.reduce(function (sushiPeople, person) {
if (person.dinner == "sushi") {
return sushiPeople.concat(person);
} else {
return sushiPeople
}
}, []); // => [{ "name": "john", "dinner": "sushi" }]
I'm sure you are able to generalize this to arbitrary keys and values!
我确信您能够将其推广到任意键和值!
#2
17
jQuery has a built-in method jQuery.grep
that works similarly to the ES5 filter
function from @adamse's Answer and should work fine on older browsers.
jQuery有一个内置的jQuery方法。grep的作用类似于@adamse给出的ES5过滤函数,并且应该在较旧的浏览器上运行良好。
Using adamse's example:
使用adamse的例子:
var peoples = [
{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
];
you can do the following
您可以执行以下操作。
jQuery.grep(peoples, function (person) { return person.dinner == "sushi" });
// => [{ "name": "john", "dinner": "sushi" }]
#3
9
If you're going to be doing this search frequently, consider changing the format of your object so dinner actually is a key. This is kind of like assigning a primary clustered key in a database table. So, for example:
如果你要经常做这个搜索,考虑改变你的对象的格式,所以晚餐实际上是一个关键。这有点像在数据库表中分配主聚集键。举个例子:
Obj = { 'pizza' : { 'name' : 'bob' }, 'sushi' : { 'name' : 'john' } }
You can now easily access it like this: Object['sushi']['name']
您现在可以轻松访问它,如下所示:Object['sushi']['name']
Or if the object really is this simple (just 'name' in the object), you could just change it to:
或者如果对象真的是这个简单的(在对象中只是“name”),您可以将其改为:
Obj = { 'pizza' : 'bob', 'sushi' : 'john' }
And then access it like: Object['sushi']
.
然后像:Object['sushi']一样访问它。
It's obviously not always possible or to your advantage to restructure your data object like this, but the point is, sometimes the best answer is to consider whether your data object is structured the best way. Creating a key like this can be faster and create cleaner code.
显然,像这样重构数据对象并不总是可能的,或者对您有利,但问题是,有时最好的答案是考虑您的数据对象的结构是否是最好的。创建这样的键可以更快,并创建更干净的代码。
#4
8
var getKeyByDinner = function(obj, dinner) {
var returnKey = -1;
$.each(obj, function(key, info) {
if (info.dinner == dinner) {
returnKey = key;
return false;
};
});
return returnKey;
}
jsFiddle。
So long as -1
isn't ever a valid key.
只要-1不是一个有效的键。
#5
1
You can find the object in array with Alasql library:
您可以通过Alasql库找到数组中的对象:
var data = [ { name : "bob" , dinner : "pizza" }, { name : "john" , dinner : "sushi" },
{ name : "larry", dinner : "hummus" } ];
var res = alasql('SELECT * FROM ? WHERE dinner="sushi"',[data]);
Try this example in jsFiddle.
在jsFiddle尝试这个示例。
#6
1
You can use a simple for in loop:
你可以使用一个简单的for in循环:
for (prop in Obj){
if (Obj[prop]['dinner'] === 'sushi'){
// Do stuff with found object. E.g. put it into an array:
arrFoo.push(Obj[prop]);
}
}
The following fiddle example puts all objects that contain dinner:sushi
into an array:
下面的小提琴示例将所有包含晚餐的对象:寿司放入一个数组中:
https://jsfiddle.net/3asvkLn6/1/
https://jsfiddle.net/3asvkLn6/1/
#7
1
There's already a lot of good answers here so why not one more, use a library like lodash or underscore :)
这里已经有很多很好的答案了,所以为什么不使用一个像lodash或下划线这样的库呢?
obj = {
1 : { name : 'bob' , dinner : 'pizza' },
2 : { name : 'john' , dinner : 'sushi' },
3 : { name : 'larry', dinner : 'hummus' }
}
_.where(obj, {dinner: 'pizza'})
>> [{"name":"bob","dinner":"pizza"}]
#1
168
If you have an array such as
如果你有一个数组,比如
var people = [
{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
];
You can use the filter
method of an Array object:
可以使用数组对象的筛选方法:
people.filter(function (person) { return person.dinner == "sushi" });
// => [{ "name": "john", "dinner": "sushi" }]
In newer JavaScript implementations you can use a function expression:
在较新的JavaScript实现中,可以使用函数表达式:
people.filter(p => p.dinner == "sushi")
// => [{ "name": "john", "dinner": "sushi" }]
You can search for people who have "dinner": "sushi"
using a map
你可以用地图搜索吃“晚餐”的人:“寿司”
people.map(function (person) {
if (person.dinner == "sushi") {
return person
} else {
return null
}
}); // => [null, { "name": "john", "dinner": "sushi" }, null]
or a reduce
或减少
people.reduce(function (sushiPeople, person) {
if (person.dinner == "sushi") {
return sushiPeople.concat(person);
} else {
return sushiPeople
}
}, []); // => [{ "name": "john", "dinner": "sushi" }]
I'm sure you are able to generalize this to arbitrary keys and values!
我确信您能够将其推广到任意键和值!
#2
17
jQuery has a built-in method jQuery.grep
that works similarly to the ES5 filter
function from @adamse's Answer and should work fine on older browsers.
jQuery有一个内置的jQuery方法。grep的作用类似于@adamse给出的ES5过滤函数,并且应该在较旧的浏览器上运行良好。
Using adamse's example:
使用adamse的例子:
var peoples = [
{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
];
you can do the following
您可以执行以下操作。
jQuery.grep(peoples, function (person) { return person.dinner == "sushi" });
// => [{ "name": "john", "dinner": "sushi" }]
#3
9
If you're going to be doing this search frequently, consider changing the format of your object so dinner actually is a key. This is kind of like assigning a primary clustered key in a database table. So, for example:
如果你要经常做这个搜索,考虑改变你的对象的格式,所以晚餐实际上是一个关键。这有点像在数据库表中分配主聚集键。举个例子:
Obj = { 'pizza' : { 'name' : 'bob' }, 'sushi' : { 'name' : 'john' } }
You can now easily access it like this: Object['sushi']['name']
您现在可以轻松访问它,如下所示:Object['sushi']['name']
Or if the object really is this simple (just 'name' in the object), you could just change it to:
或者如果对象真的是这个简单的(在对象中只是“name”),您可以将其改为:
Obj = { 'pizza' : 'bob', 'sushi' : 'john' }
And then access it like: Object['sushi']
.
然后像:Object['sushi']一样访问它。
It's obviously not always possible or to your advantage to restructure your data object like this, but the point is, sometimes the best answer is to consider whether your data object is structured the best way. Creating a key like this can be faster and create cleaner code.
显然,像这样重构数据对象并不总是可能的,或者对您有利,但问题是,有时最好的答案是考虑您的数据对象的结构是否是最好的。创建这样的键可以更快,并创建更干净的代码。
#4
8
var getKeyByDinner = function(obj, dinner) {
var returnKey = -1;
$.each(obj, function(key, info) {
if (info.dinner == dinner) {
returnKey = key;
return false;
};
});
return returnKey;
}
jsFiddle。
So long as -1
isn't ever a valid key.
只要-1不是一个有效的键。
#5
1
You can find the object in array with Alasql library:
您可以通过Alasql库找到数组中的对象:
var data = [ { name : "bob" , dinner : "pizza" }, { name : "john" , dinner : "sushi" },
{ name : "larry", dinner : "hummus" } ];
var res = alasql('SELECT * FROM ? WHERE dinner="sushi"',[data]);
Try this example in jsFiddle.
在jsFiddle尝试这个示例。
#6
1
You can use a simple for in loop:
你可以使用一个简单的for in循环:
for (prop in Obj){
if (Obj[prop]['dinner'] === 'sushi'){
// Do stuff with found object. E.g. put it into an array:
arrFoo.push(Obj[prop]);
}
}
The following fiddle example puts all objects that contain dinner:sushi
into an array:
下面的小提琴示例将所有包含晚餐的对象:寿司放入一个数组中:
https://jsfiddle.net/3asvkLn6/1/
https://jsfiddle.net/3asvkLn6/1/
#7
1
There's already a lot of good answers here so why not one more, use a library like lodash or underscore :)
这里已经有很多很好的答案了,所以为什么不使用一个像lodash或下划线这样的库呢?
obj = {
1 : { name : 'bob' , dinner : 'pizza' },
2 : { name : 'john' , dinner : 'sushi' },
3 : { name : 'larry', dinner : 'hummus' }
}
_.where(obj, {dinner: 'pizza'})
>> [{"name":"bob","dinner":"pizza"}]