admin管理员组

文章数量:1377511

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

$slides.findInArray('desc', '127').addValueToObject('in','yes')

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

$slides.findInArray('desc', '127').addValueToObject('in','yes')
Share Improve this question asked Oct 18, 2012 at 2:49 technopeasanttechnopeasant 7,95933 gold badges93 silver badges151 bronze badges 5
  • 1 What is with the $ in the name? Basically you need to loop and pare. – epascarello Commented Oct 18, 2012 at 2:53
  • 1 it's a global variable; name doesn't much matter though. loop and pare? – technopeasant Commented Oct 18, 2012 at 2:54
  • 1 loop...aka for loop. look at each index of the array. Compare aka x==y, check each object's property for the value? Seems like a simple solution. The dollar sign makes no sense, sounds like you re trying to make JavaScript feel like another language with it there. – epascarello Commented Oct 18, 2012 at 2:59
  • I usually add a dollar sign at the beginning of a variable if it's a jquery object, to remind myself that it's shorthand, for example $this = $(this). Otherwise, yeah. Looks like you're trying to make it look like a different language. – Jan Commented Oct 18, 2012 at 3:04
  • oh, right on. that's a great point. – technopeasant Commented Oct 18, 2012 at 3:07
Add a ment  | 

4 Answers 4

Reset to default 3

http://jsfiddle/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 paring 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';
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/s6ThK/

You need to run it through a for loop

// 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;
    }
}

With modern JS it can be simply done:

var obj = $slides.find(e => e.desc === '127');
if (obj) {
    obj.in = 'yes';
}

本文标签: javascript find object in array by value and append additional valueStack Overflow