I am trying to think of a cleaner way to do this, basically loop through an object and if name matches a keyword do soemthing or if no match is found do something else.
var obj = {
0: {
name: 'test'
},
1: {
name: 'test2'
},
2: {
name: 'test3'
},
3: {
name: 'test4'
}
};
var len = (Object.keys(obj).length - 1);
var valuetomatch = 'test2';
while (len > -1) {
if (obj[len].name === valuetomatch) {
alert('match: ' + obj[len].name);
break;
} else if (len === 0) {
alert('no match found!');
}
len -= 1;
}
/
I am trying to think of a cleaner way to do this, basically loop through an object and if name matches a keyword do soemthing or if no match is found do something else.
var obj = {
0: {
name: 'test'
},
1: {
name: 'test2'
},
2: {
name: 'test3'
},
3: {
name: 'test4'
}
};
var len = (Object.keys(obj).length - 1);
var valuetomatch = 'test2';
while (len > -1) {
if (obj[len].name === valuetomatch) {
alert('match: ' + obj[len].name);
break;
} else if (len === 0) {
alert('no match found!');
}
len -= 1;
}
https://jsfiddle/fw4svrck/
Share Improve this question edited Apr 8, 2016 at 19:28 Elmo 6,48117 gold badges79 silver badges143 bronze badges asked Apr 8, 2016 at 19:24 user1572796user1572796 1,0572 gold badges21 silver badges48 bronze badges 5- Is there a reason for this to be an object of objects indexed like that, instead of being an array of objects? – dustinroepsch Commented Apr 8, 2016 at 19:29
- 1 Since your main object has numeric properties, you really should structure it as an array, which will assign the indexes as you have them automatically. Your array would then consist of object elements. – Scott Marcus Commented Apr 8, 2016 at 19:32
-
And if you make it a real array, you can then use
obj.some(...)
instead ofObject.keys(obj).some
like in the answers. – Barmar Commented Apr 8, 2016 at 19:38 - if you call this more often, you can create a second object, where the objects are mapped by their name. then you can simply check wether the name in in this map. – Thomas Commented Apr 8, 2016 at 19:50
- thanks for the replies, this is a very simplified version of what Im working on, so the keys are not always numbers, etc – user1572796 Commented Apr 9, 2016 at 2:38
5 Answers
Reset to default 4You could use Array#some()
, if you are sure, that only one item is to search.
var obj = { 0: { name: 'test' }, 1: { name: 'test2' }, 2: { name: 'test3' }, 3: { name: 'test4' } },
valuetomatch = 'test2',
found = Object.keys(obj).some(function (k) {
if (obj[k].name === valuetomatch) {
// do something
return true;
}
});
document.write(found);
This is another way of searching desired item in an object.
var obj = {
0: {
name: 'test'
},
1: {
name: 'test2'
},
2: {
name: 'test3'
},
3: {
name: 'test4'
}
};
var len = Object.keys(obj).length;
var valuetomatch = 'test2';
var flag=false;
for(i=0;i<len;i++){
if(obj[i]["name"]==valuetomatch){
flag=true;
alert(valuetomatch+" is found");
}
}
if(flag===false)
alert(valuetomatch+" is not found");
Don't use this, @Nina's solution is much more efficient.
Using the Array#forEach
method instead of a while loop makes it much simpler:
var obj = {
0: {
name: 'test'
},
1: {
name: 'test2'
},
2: {
name: 'test3'
},
3: {
name: 'test4'
}
};
var valuetomatch = 'test2';
var match;
Object.keys(obj).forEach(function(key) {
if (obj[key].name === valuetomatch) {
match = true;
}
});
if (match) {
alert('match: ' + valuetomatch);
} else {
alert('no match found!');
}
You can use for..in
loop for iterating through an object
var obj = { 0: { name: 'test' }, 1: { name: 'test2' }, 2: { name: 'test3' }, 3: { name: 'test4' } };
var valuetomatch = 'test2';
var matched = false;
for(var v in obj) {
if(obj.hasOwnProperty(v) && obj[v].name == valuetomatch) {
alert('match: ' + obj[v].name);
matched = true;
break;
}
}
if (!matched) {
alert('no match found!');
}
for/in loop is an efficient way to go here.
I've re-factored isvforall for/in solution above into a function that you can re-use for other nested objects with similar structure. toMatch(obj, prop, value) takes 3 parameters: obj (the object you're traversing through), prop (the property you're filtering), and value (the value you're searching a match for). Can be re-factored further based on data structure.
var obj1 = {
0: {
name: 'test'
},
1: {
name: 'test2'
},
2: {
name: 'test3'
},
3: {
name: 'test4'
}
};
var toMatch = function(obj, prop, value) {
var foundMatch = false;
for (var key in obj) {
if (obj.hasOwnProperty(key) && obj[key][prop] === value) {
alert('Found match! ' + value + ' is found at key ' + key);
foundMatch = true;
break;
}
}
if (!foundMatch) {
alert('no match found!');
}
};
toMatch(obj1, 'name', 'test2');
// => Found match! test2 is found at key 1