2

I read more here, but I have not found the answer to my problem My problem:

I have (from php via ajax return json string)

str = '[{"id":"159","pavadinimas":"Building B","_id":"200",.................}]';

this is JSON string and I do obj = $.parseJSON(str); i can take any value in that way like alert(obj[0]._id) I get 200.

But how I can get associative array from json like:

dataArray = [
"id":"159",
"pavadinimas":"Building B",
"_id":"200",
...
]

I want get value 200 like that

val = dataArray['_id'];
2
  • dataArray = obj[0];. obj is actually an array of objects, so if you want to have a reference to the first object, you just assign it to a variable. Commented Mar 13, 2013 at 8:05
  • You've just randomly invented some non-Javascript syntax. Why? Commented Mar 13, 2013 at 8:19

5 Answers 5

8

There is no associative array concept in javascript, but only object.

var arr = $.parseJSON(str);
var obj = arr[0];
var _id = obj['_id']
Sign up to request clarification or add additional context in comments.

1 Comment

and all objects in JS is for all purposes an associative array
4

Javascript does not have associative arrays. You will have to use objects, which use curly brackets {} instead.

dataObj = {
    "id":"159",
    "pavadinimas":"Building B",
    "_id":"200",
    ...
}

console.log( dataobj.id );
console.log( dataobj['id'] );

Both works the same.

So for your example, you can access the object the way you would expect an associative array to work.

Comments

1

Any object in JavaScript is to all purposes an associative array

e.g. you can do

dataArray = obj[0];
for(var prop in dataArray) {
   if(dataArray.hasOwnProperty(prop)){
      console.log(dataArray[prop];
   }
}

which will print the value of each object to the console. The call to hasOwnProperty is not really needed in you case since the object you are getting due to it's prototype but it is generally recommended than when iterating over the properties of an object to check if it belongs to the object it self or an object somewhere up the prototype chain

For almost (1) any property of an object there's two semantically equvalent ways to accessing the value

obj.propertyName
obj["propertyName"]

will both yield the same value.

(1) When the property name is not a valid identifier. I.e you can do

obj["property name"]

but can't do

obj.property name

In other words property names do not have to be valid identifiers.

2 Comments

Did you intend to put a dot in obj.["propertyName"] ? Doesn't look right in regards of the lower line and MildlySerious's answer!
@JamesCameron Nope that's a left over from doing F# programming I guess. Thanks for spotting it :)
0

Use .map()

for (var i in elements)
{
    elements[i] = $.map(elements[i],function(v)
    {
        return v;
    });
}     

Comments

0

May be you want something like this:

 var idArray = [];
 var buildArray = [];
 var _idArray = [];
 var data = [{
              "id": 159,
              "pavadinimas": "Building A",
              "_id": 100
            }, {
              "id": 160,
              "pavadinimas": "Building B",
              "_id": 200
            }, {
              "id": 161,
              "pavadinimas": "Building C",
              "_id": 400
           }];

 $.each(data, function (i, item) {
    idArray.push(item.id);
    buildArray.push(item.pavadinimas);
    _idArray.push(item._id);
 });

 console.log(idArray[0]+' : '+buildArray[0]+ ' : ' +_idArray[0]);

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.