I think the title explains it well enough. I've got an array that has two values per object and I need to look up the object by one of those values then assign a third to it.
我认为这个标题很好地解释了这一点。我有一个数组,每个对象有两个值,我需要通过其中一个值查找对象,然后给它分配第三个值。
Here are the guts:
这里有勇气:
$slides.push({
img: el.attr('href'),
desc: el.attr('title').split('Photo #')[1]
});
Which builds an array as such:
建立这样一个数组:
Object
desc: 127
img: img/aaron1.jpg
Object
desc: 128
img: img/aaron2.jpg
I'd like to look up the desc
value, then assign a third value of in: yes
我想查找desc值,然后分配第三个值in: yes
$slides.findInArray('desc', '127').addValueToObject('in','yes')
3 个解决方案
#1
3
http://jsfiddle.net/S3cpa/
var test = [
{
desc: 127,
img: 'img/aaron1.jpg',
},
{
desc: 128,
img: 'img/aaron2.jpg',
}
];
function getObjWhenPropertyEquals(prop, val)
{
for (var i = 0, l = test.length; i < l; i++) {
// check the obj has the property before comparing it
if (typeof test[i][prop] === 'undefined') continue;
// if the obj property equals our test value, return the obj
if (test[i][prop] === val) return test[i];
}
// didn't find an object with the property
return false;
}
// look up the obj and save it
var obj = getObjWhenPropertyEquals('desc', 127);
// set the new property if obj was found
obj.in = obj && 'yes';
#2
1
easy way
for (var i = 0; i < $slides.length; i++)
{
if ($slides[i]["desc"] == "TEST_VALUE")
{
$slides[i]['in']='yes';
}
}
Another way
Array.prototype.findInArray =function(propName,value)
{
var res={};
if(propName && value)
{
for (var i=0; i<this.length; i++)
{
if(this[i][propName]==value)
{
res = this[i];
break;
}
}
}
return res;
}
Object.prototype.addValueToObject =function(prop,value)
{
this[prop]=value;
}
---Using It--
———使用它
$slides.findInArray('desc', '127').addValueToObject('in','yes');
http://jsfiddle.net/s6ThK/
#3
1
You need to run it through a for loop
您需要通过for循环来运行它。
// Loop through the array
for (var i = 0 ; i < $slides.length ; i++)
{
// Compare current item to the value you're looking for
if ($slides[i]["desc"] == myValue)
{
//do what you gotta do
$slides[i]["desc"] = newValue;
break;
}
}
#1
3
http://jsfiddle.net/S3cpa/
var test = [
{
desc: 127,
img: 'img/aaron1.jpg',
},
{
desc: 128,
img: 'img/aaron2.jpg',
}
];
function getObjWhenPropertyEquals(prop, val)
{
for (var i = 0, l = test.length; i < l; i++) {
// check the obj has the property before comparing it
if (typeof test[i][prop] === 'undefined') continue;
// if the obj property equals our test value, return the obj
if (test[i][prop] === val) return test[i];
}
// didn't find an object with the property
return false;
}
// look up the obj and save it
var obj = getObjWhenPropertyEquals('desc', 127);
// set the new property if obj was found
obj.in = obj && 'yes';
#2
1
easy way
for (var i = 0; i < $slides.length; i++)
{
if ($slides[i]["desc"] == "TEST_VALUE")
{
$slides[i]['in']='yes';
}
}
Another way
Array.prototype.findInArray =function(propName,value)
{
var res={};
if(propName && value)
{
for (var i=0; i<this.length; i++)
{
if(this[i][propName]==value)
{
res = this[i];
break;
}
}
}
return res;
}
Object.prototype.addValueToObject =function(prop,value)
{
this[prop]=value;
}
---Using It--
———使用它
$slides.findInArray('desc', '127').addValueToObject('in','yes');
http://jsfiddle.net/s6ThK/
#3
1
You need to run it through a for loop
您需要通过for循环来运行它。
// Loop through the array
for (var i = 0 ; i < $slides.length ; i++)
{
// Compare current item to the value you're looking for
if ($slides[i]["desc"] == myValue)
{
//do what you gotta do
$slides[i]["desc"] = newValue;
break;
}
}