1

I have an array of 4 objects in the response like:

enter image description here

In each object I have a properties array of 8 objects

enter image description here

I am trying to assign for each property value of object to the properties value of other object having the same structure.

enter image description here

I am directly assigning this way and its now working.

 $scope.details.tabs = response.data.tabs;

there is a way we can do it using AngularJS forEach loop and assign properties values to other object having same structure. I am new to AngularJS, can anybody help to reach object properties value?

Ahmad Hosny
  • 597
  • 1
  • 6
  • 23
Harshit
  • 397
  • 2
  • 6
  • 17
  • can use .map to assign new obj inside the for loop. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map , guessing you want to add new obj in the array – MrNew Apr 04 '16 at 07:35
  • $scope.details.tabs and response.data.tabs having same structure. I am trying to assign a value to other .I think new object will not require. – Harshit Apr 04 '16 at 07:39
  • I don't get why you want to do it using a loop while loops take more time to execute and all you want is to copy the array of objects. Am I misunderstanding something? – Ahmad Hosny Apr 04 '16 at 07:41
  • @Ahmad. Actually I want to assign only value field and do not want to touch other fields as they have some other significance,So thought loop will be the best way to traverse. Do you have any other best way to do it? – Harshit Apr 04 '16 at 07:48
  • A normal JavaScript `for` loop would do – Ahmad Hosny Apr 04 '16 at 07:55
  • @Ahmad. Thanks Can you help in traversing to that level? – Harshit Apr 04 '16 at 08:09
  • Can you pass me the object structure? – Ahmad Hosny Apr 04 '16 at 08:16
  • @AhmadHosny .. its same as in the images.do you need more info? – Harshit Apr 04 '16 at 08:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108153/discussion-between-ahmad-hosny-and-harshit). – Ahmad Hosny Apr 04 '16 at 08:25

1 Answers1

1

Here is an example to what you need to do (Note: This is a plain Javascript solution):

var itemArray = response.data.tabs;
var newArray = [];
var newArraySize = 0;
for (var i = 0; i < itemArray.length; i++) {
    for (var j = 0; j < itemArray[i].properties.length; j++) {
        newArray[newArraySize] = itemArray[i].properties[j];
        newArraySize ++;
    }
}

This way you will have newArray containing all of the properties objects. If you need the values only (as a string), all you need to do is to dig one level deeper using the . operator, like this:

newArray[newArraySize] = itemArray[i].properties[j].value;

Hint: I assigned the reponse.data.tabs array to another array itemArray just for making the name shorter and to speed up my typing. You may consider removing this step if you want.

I'm sure there are other solutions but this is what came up to my mind so far. Hope it works for you :)

Ahmad Hosny
  • 597
  • 1
  • 6
  • 23
  • thanks alot :) suppose .. my new array already having the array structure as itemArray called assignArray. in place of this newArray[newArraySize] = itemArray[i].properties[j]; i want to assign the value of itemArray.properties.value to assignArray.properties.value .Is this possible with the above snippet? – Harshit Apr 04 '16 at 09:46
  • Yes, with changes: the line you're looking for might be ( according to my understanding of the new question ;) ) assignArray[i].properties[j].value = itemArray[i].properties[j].value; . Type this line in the inner loop. – Ahmad Hosny Apr 04 '16 at 10:02