0

I have an array like below.

[{
  'i_id':'119',
  'name':'Brinjal-250',
  'weight':'250.Gm',
  'qty':'1',
  'price':'5',
  'market':'55',
  'image':'f_control/images/vegetables/Brinjal-PNG-Image-min'
 },   
 {  
  'i_id':'101',
  'name':'Tomato',
  'weight':'500.Gm',
  'qty':'1',
  'price':'4',
  'market':'44',
  'image':'f_control/images/vegetables/tometo-min.jpg'
}]

And i want it to in for each loop using java script, It is possible? If possible then please guide me in right direction, Thanks.

The demo outputenter image description here

DroidNoob
  • 1,123
  • 9
  • 21
Vishal Panara
  • 233
  • 3
  • 15

3 Answers3

1

Why are you using different array for each object ? You can push both objects in a single array , then you can use forEach loop like this :-`

var array = [
    {
        'i_id':'119',
        'name':'Brinjal-250',
        'weight':'250.Gm',
        'qty':'1',
        'price':'5',
        'market':'55',
        'image':'f_control/images/vegetables/Brinjal-PNG-Image-min'
    },
    {
        'i_id':'101',
        'name':'Tomato',
        'weight':'500.Gm',
        'qty':'1',
        'price':'4',
        'market':'44',
        'image':'f_control/images/vegetables/tometo-min.jpg'
    }
    ];
        array.forEach(function(value){
            console.log(value.name);
        });`
Ashish Kumar
  • 212
  • 1
  • 7
  • Thanks, But How can i push both objects in single array? – Vishal Panara Mar 29 '17 at 05:17
  • when you are making these objects on the backend. – Ashish Kumar Mar 29 '17 at 05:19
  • You may try Array.prototype.concat() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat – Mary Mar 29 '17 at 05:20
  • @Mary will concating the arrays is an efficient method ? because on the client end if there are thousands of arrays and you have to concat them will take time and memory also because after concating these arrays it returns a new array. – Ashish Kumar Mar 29 '17 at 05:23
  • @AshishKumar you may check discussion about concat efficiency here: http://stackoverflow.com/questions/5080028/what-is-the-most-efficient-way-to-concatenate-n-arrays-in-javascript – Mary Mar 29 '17 at 05:29
  • @VishalPanara please upvote the answer if you found it useful . thanks. – Ashish Kumar Mar 29 '17 at 05:30
  • @Mary i request you to please go through your link once again. In your link the answer is that if there are many arrays more than (100 ,000) the best approach is to use forloop in which you can push one array in to another array. And its also written there is .concat() is a method for convenience and likely performance not for the best performance – Ashish Kumar Mar 29 '17 at 05:38
  • @AshishKumar all true. They say that method is better with the big amount of items. In case that not many people will reach this comment discussion on the bottom of the page I suggest you to edit your question one more time and add all your issues there. Tell about your big amount of arrays of objects (btw, are your arrays of objects are come in a single array?), efficiency, and tell about methods you found but can't ore don't want to use (like concat). Because it look like a request about forEach JS syntax for now. – Mary Mar 29 '17 at 05:52
1

If both objects are in the same array, just like below:

[{'i_id':'119',
'name':'Brinjal-250',
'weight':'250.Gm',
'qty':'1',
'price':'5',
'market':'55',
'image':'f_control/images/vegetables/Brinjal-PNG-Image-min'},
{'i_id':'101',
    'name':'Tomato',
    'weight':'500.Gm',
    'qty':'1',
    'price':'4',
    'market':'44',
    'image':'f_control/images/vegetables/tometo-min.jpg'}]

then use:

arrayName.forEach(function (item, index) {
  console.log(item["name"]);
})

or else use:

for(var key in arrayName) {
   console.log("key: "+key+" Value: "+arrayName[key]["name"]);
}

Check out the fiddle : https://jsfiddle.net/NayanaDas/42f3yxho/

Updated Answer:

if you want to make this array [{'i_id':'101'}] [{'i_id':'102'}] like this [{'i_id':'101'},{'i_id':'102'}] , then use JavaScript Array concat() Method :

var arr1 = [{'i_id':'101'}];
var arr2 = [{'i_id':'102'}];

var array = arr1.concat(arr2);

console.log(JSON.stringify(array));

See the fiddle : https://jsfiddle.net/NayanaDas/eaumhnvw/

Nayana_Das
  • 1,789
  • 4
  • 23
  • 48
0

The requirement is not clear. If you want to loop through he array use forEach or conventinal for-loop will also work.

var myArray=[//json array]
myArray.forEach(function(item,index){
  console.log(item.name) // will seperately log Brinjal-250, & Tomato
})
brk
  • 48,835
  • 10
  • 56
  • 78