1

I have an object on my scope, the_data, which has multiple objects inside it.

One object, important_entries has a list of objects inside it.

The objects inside important_entries all have a specific field I want to change - field_I_want_to_change. The name of each of these objects is it's Id.

What I want to do is iterate over the important_entries object and for each object in it, change the value of each field_I_want_to_change field.

Could someone possibly give me some pointers as to how best to accomplish this? I have access to the underscore library, if that can be of any use.

Here is a rough blueprint of my the_data object, I apologise for any syntax errors.

the_data : {
    some_data : {},
    some_data : {},
    important_entries: {
        xxxccc : {
            field_I_want_to_change: 'some data'
        },
        cccfff : {
            field_I_want_to_change: 'some data'
        },
        tttyyy : {
            field_I_want_to_change: 'some data'
        },
    },
    some_data : {},
    some_data : {}
}
Daft
  • 10,277
  • 15
  • 63
  • 105
  • What did you try? (Underscore seems like a good first step) – doldt Aug 05 '15 at 12:13
  • @doldt if I have a single object in 'important_entries', it's not a problem. Just when there are multiple objects I trip up. – Daft Aug 05 '15 at 12:20

2 Answers2

4

Using the underscore lib

var thedata =  {
    some_data : {},
    some_data : {},
    important_entries: {
        xxxccc : {
            field_I_want_to_change: 'some data'
        },
        cccfff : {
            field_I_want_to_change: 'some data'
        },
        tttyyy : {
            field_I_want_to_change: 'some data'
        },
    },
    some_data : {},
    some_data : {}
}

 _.each(thedata.important_entries, function(entry) {
    entry.field_I_want_to_change = "new calue"
 })

If you are not sure if the "field_I_want_to_change" object exists always, through in a small check before with _.has

AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
2

Why don't you loop through javascript object and change the field?

p = the_data['important_entries'];
for (var key in p) {
    if (p.hasOwnProperty(key)) {
        p[key].field_I_want_to_change = 'some data';
    }
}
Community
  • 1
  • 1
Zim84
  • 3,404
  • 2
  • 35
  • 40