1

Hear an Object Array

 advancedParams = [];
 advancedParams["cost"] = [];
 advancedParams["cost"]["from"] = 10;
 advancedParams["cost"]["to"] = 100;

But the .each() do not see second array

 $.each(advancedParams, function( idx, obj ) {
     console.log(idx); // shows cost
     console.log(obj ); // shows [from: "10", to: "200"]
     $.each(obj, function( key, value ) {
         console.log(key); // nothing
         //each have no iterations, why?
     });
 }
2
  • 4
    {cost: [from: "10"], [to: "200"]} i guess it is invalid. Commented Mar 19, 2015 at 8:03
  • Find problem. Array binding by advancedParams = {}, but the second was bind by advancedParams[param] = [], when type advancedParams[param] = {} it start work. Commented Mar 19, 2015 at 8:13

2 Answers 2

2

This is absolutely invalid:

{cost: [from: "10"], [to: "200"]}

because in an array you can't have values with : separated.

Instead of declaring an array [] you should assign an object.

advancedParams = {};
advancedParams["cost"] = {};

check in the demo below.

var advancedParams = {};
advancedParams["cost"] = {};
advancedParams["cost"]["from"] = 10;
advancedParams["cost"]["to"] = 100;

$.each(advancedParams, function(i, obj) {
  console.log(obj); // results in "Object {from: 10, to: 100}"
  $.each(obj, function(i, item) {
    console.log(item); // results in "10, 100"
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Update array. Simply I tried to explain more simply.
Yes. Problem was in advancedParams["cost"] = [ ]
0

Your object definition it's invalid. Look at the error message below :

invalid object definition

Store it in an object like this : var obj = { from: 10, to:200}. Or if you want an array of objects, do it like this: var arr =[{from:10,to:200},{from:1, to:300}].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.