2

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')
technopeasant
  • 7,809
  • 31
  • 91
  • 149
  • 1
    What is with the $ in the name? Basically you need to loop and compare. – epascarello Oct 18 '12 at 02:53
  • 1
    it's a global variable; name doesn't much matter though. loop and compare? – technopeasant Oct 18 '12 at 02: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 Oct 18 '12 at 02: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 Oct 18 '12 at 03:04
  • oh, right on. that's a great point. – technopeasant Oct 18 '12 at 03:07

4 Answers4

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';
Matt Stone
  • 3,705
  • 4
  • 23
  • 40
1

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;
    }
}
Jan
  • 5,688
  • 3
  • 27
  • 44
  • continue will simply jump to the next loop iteration. You need to use the break; statement to exit the loop. – Matt Stone Oct 18 '12 at 04:05
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/

Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
1

With modern JS it can be simply done:

var obj = $slides.find(e => e.desc === '127');
if (obj) {
    obj.in = 'yes';
}
Alex Che
  • 6,659
  • 4
  • 44
  • 53