5

I am just curious about this.

Let's say I have an array of objects and I create 1 object, lets name the array of objects items and the object item.

I want to get a particular item in my array of items by using the following code:

//gets an item base on ID
function get_item(td){
    var item = undefined;
    $.each(items, function(i, val) {
        if(val.item_id == td){
            item = val;
        }   
    });
    return item;
}

The get_item() basically gets an object matched with the supplied id.

So my question is this. What if I changed the properties of item will it also changed the properties of an object associated with it within the array?

Thank you very much!

7
  • Could you post sample array object? Commented Jun 1, 2015 at 4:23
  • 3
    show more code. Right after your "let's say I have...", show the code that you think corresponds to that situation. Commented Jun 1, 2015 at 4:23
  • 3
    if you are changing in item with reference to the array then the item will change. Commented Jun 1, 2015 at 4:25
  • 3
    If you are returning the same instance as stored in the array... yes it will Commented Jun 1, 2015 at 4:25
  • 1
    @JCFrane in this format yes it will change Commented Jun 1, 2015 at 4:26

3 Answers 3

6

What if I changed the properties of item will it also changed the properties of an object associated with it within the array?

Yes.

Objects are not copied. Instead, references to the objects are passed around. Simplest example:

var a = [];
var b = a;
b.push(1);
console.log(a); // logs [1]

Many object-oriented programming languages work like this.

Sign up to request clarification or add additional context in comments.

Comments

2

The value of the object inside the array will also change because it's a reference. If you want more information I highly recommend reading Objects and Prototypes.

If you don't want it to change then you should use something like lodash's _.clone() function.

Also you could use filter to get the object:

function get_item(td){
    return items.filter(function(item) {
        return item.id === td;
    })[0];
}

Comments

1

You can update you function to:

var data= array();
function get_item(propertyValue, propertyName){
    var retval;
    for(var i = 0; i < data.length; i++){
         if(data[i][propertyName]==propertyValue){
            retval = data[i];
            break;
         }
    }
    return retval;
}

Use it

var item1 = get_item(1,"id");
var item2 = get_item("john","name");

4 Comments

can you please elaborate more?
How does this answer the OP's question? "What if I changed the properties of item will it also changed the properties of an object associated with it within the array?"
It does not answer, but it helps.
@JCFrane: There are many things that help. That doesn't mean that it is appropriate to post about them. As the term already implies, "answers" should answer the question that was posed. As the term implies, "answers" should answer the question that was posed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.