I need to perhaps write a function that just outputs the index of an object inside an array, obviously, using $.inArray returns this just fine in the example below.
array = ['one', 'two', 'three'];
$.inArray('one', array) // 0
With a more elaborate array, How can I find the index of the objects nested within?
array = [
{
name: 'One', // index??
data: {
title: 'Title One',
content: 'Content One'
}
},
{
name: 'Two',
data: {
title: 'Title Two',
content: 'Content Two'
}
},
{
name: 'Three',
data: {
title: 'Title Three',
content: 'Content Three'
}
}
];
I've heard of the $.grep() method, indexOf() .. not sure which one to use to just return an integer of the index the object is in
I need to perhaps write a function that just outputs the index of an object inside an array, obviously, using $.inArray returns this just fine in the example below.
array = ['one', 'two', 'three'];
$.inArray('one', array) // 0
With a more elaborate array, How can I find the index of the objects nested within?
array = [
{
name: 'One', // index??
data: {
title: 'Title One',
content: 'Content One'
}
},
{
name: 'Two',
data: {
title: 'Title Two',
content: 'Content Two'
}
},
{
name: 'Three',
data: {
title: 'Title Three',
content: 'Content Three'
}
}
];
I've heard of the $.grep() method, indexOf() .. not sure which one to use to just return an integer of the index the object is in
Share Improve this question asked Jul 26, 2012 at 2:01 Jeff VossJeff Voss 3,69510 gold badges49 silver badges72 bronze badges 1- None of those functions handle multi-dimensional arrays. You'll have to roll your own search function. indexof/grep can be used at each level of the array, but moving between levels is up to you. – Marc B Commented Jul 26, 2012 at 2:04
3 Answers
Reset to default 5You don't need a pre-written function, just iterate over your array and pare the name
property:
function findValue(array, nameWeAreLookingFor) {
for(var i = 0; i<array.length; i++) {
if(array[i].name === nameWeAreLookingFor) return i;
}
return -1;
}
No built in functions.. but it's easy to write all flavours of your own. And as an exercise you can also use them to extend jQuery,
var array = [{
name: 'One',
// index??
data: {
title: 'Title One',
content: 'Content One'
}},
{
name: 'Two',
data: {
title: 'Title Two',
content: 'Content Two'
}},
{
name: 'Three',
data: {
title: 'Title Three',
content: 'Content Three'
}}];
function findByName(name) {
var index;
$(array).each(function(i, e) {
if (e.name && e.name == name) {
index = i;
return false;
}
});
return index;
}
console.log(findByName("One")); // prints 0
// and now even better ... find by any property
function findByProperty(objects, prop, value) {
var index;
$(objects).each(function(i, e) {
if (e[prop] && e[prop] == value) {
index = i;
return false;
}
});
return index;
}
// usage
var index = findByProperty(array, "name", "One");
console.log(index); // prints 0
index = findByProperty(array, "name", "Three");
console.log(index); // prints 2
// and even more powerful
function findByFilter(objects, filter) {
var index;
$(objects).each(function(i, e) {
if (filter(i, e)) {
index = i;
return false;
}
});
return index;
}
index = findByFilter(array,function(i,e){ return e.data.title=="Title Two"; });
console.log(index); // prints 1
You can use the name
property as index.
Try this code:
var indexOfObj = function(array,objName){
var len = array.length;
while(--len>=0 && array[len].name!==objName);
return len;
}
This function will return -1
if the obj wasn't found in the array.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362362a4429636.html
评论列表(0条)