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

11
  • 1
    what is the expected output? Commented Mar 29, 2017 at 5:00
  • Your array structure does not look good. Please update your question and try to make it more meaningful. Commented Mar 29, 2017 at 5:00
  • 1
    There are two arrays here. Update the question with correct structure and expected output Commented Mar 29, 2017 at 5:01
  • Question is not clear.. Commented Mar 29, 2017 at 5:01
  • okay, wait while i clear my question. Commented Mar 29, 2017 at 5:03

3 Answers 3

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);
        });`
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, But How can i push both objects in single array?
when you are making these objects on the backend.
@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.
@AshishKumar you may check discussion about concat efficiency here: stackoverflow.com/questions/5080028/…
|
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/

2 Comments

I am getting the array like this [{'i_id':'101'}] [{'i_id':'102'}] and i want like this [{'i_id':'101'},{'i_id':'102'}] . any suggestion?
@VishalPanara : please check out the updated answer. Hope this helps you.
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
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.