user348173
user348173

Reputation: 9278

how to cut object from array and put it in object itself

I have response object which contain array:

Cars:
[0]: {
       Name: 'Name1',
       Color: 'Color2'
     },
[1]: {
       Name: 'Name2',
       Color: 'Color2'
     },

I want to copy the first item in the array and put it to object, Cars have to look:

Cars:
     {
           Name: 'Name1',
           Color: 'Color'
      },

Upvotes: 0

Views: 52

Answers (2)

florin.prisecariu
florin.prisecariu

Reputation: 422

check this: http://jsfiddle.net/romanian/6no49uwt/

response = [{
    'Name': 'Name1',
    'Color': 'Color2'
}, {
    'Name': 'Name2',
    'Color': 'Color2'
}]

console.log(response);

firstCar = response[0];

console.log(firstCar);

for display of console.log(), you will need firebug or some dev tool

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074138

If you're depicting things in memory, and Cars refers to an array containing objects, and your goal is to have Cars refer to just the first of those objects, you can do this:

Cars = Cars[0];

If the Cars variable was the only variable with a reference to the array, then the array becomes eligible for garbage collection. If the array's reference to the other object was the only reference to that object, then that other object ([1]) also becomes eligible for garbage collection.

Some ASCII-art might help here:

At first, you have this:

                                 +-----------------+ 
+------+      +---------+   +--->|    (object)     | 
| Cars |----->| (array) |   |    +-----------------+ 
+------+      +---------+   |    | Name: 'Name1'   | 
              | 0       |---+    | Color: 'Color2' | 
              | 1       |---+    +-----------------+ 
              +---------+   |
                            |    +-----------------+
                            +--->|    (object)     |
                                 +-----------------+
                                 | Name: 'Name2'   |
                                 | Color: 'Color2' |
                                 +-----------------+

Then when you do:

Cars = Cars[0];

you have this:

+------+                           
| Cars |--------------------+
+------+                    |
                            |    +-----------------+ 
              +---------+   +--->|    (object)     | 
              | (array) |   |    +-----------------+ 
              +---------+   |    | Name: 'Name1'   | 
              | 0       |---+    | Color: 'Color2' | 
              | 1       |---+    +-----------------+ 
              +---------+   |
                            |    +-----------------+
                            +--->|    (object)     |
                                 +-----------------+
                                 | Name: 'Name2'   |
                                 | Color: 'Color2' |
                                 +-----------------+

Note that nothing is pointing to the array anymore, so if garbage collection occurs, you end up with this:

+------+      +-----------------+  
| Cars |----->|    (object)     |
+------+      +-----------------+
              | Name: 'Name1'   | 
              | Color: 'Color2' | 
              +-----------------+ 

Of course, if something else does have a reference to the array, the array wouldn't be GC'd (and similarly, if something else has a reference to the second object, it won't be GC'd). But that's what happens if we assume nothing else has those references.

Upvotes: 5

Related Questions