4

Firstly, this is my json value i am getting from a php source:

[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]

After that, I want to get and oid value along with the corresponding cid value for example, oid=2 and cid=107 at one go, oid=4 and cid=98 at another and so on. I am trying to use jquery, ajax for this.

I have tried many answers for this, like: Javascript: Getting all existing keys in a JSON array and loop and get key/value pair for JSON array using jQuery but they don't solve my problem.

I tried this:

for (var i = 0; i < L; i++) { var obj = res[i]; for (var j in obj) { alert(j); }

but all this did was to return the key name, which again did not work on being used.

Community
  • 1
  • 1
xprilion
  • 569
  • 1
  • 5
  • 14
  • Please include attempted solutions, why they didn't work, and the expected results. – palaѕн Aug 07 '13 at 12:51
  • One thing I would suggest: open up chrome's js console, and look at the variable in the debugger. Then you should be able to see what's going wrong (maybe trying to access the wrong variable, or not deep enough). – JGibbers Aug 07 '13 at 12:52

3 Answers3

9

So, you have an array of key/value pairs. Loop the array, at each index, log each pair:

var obj = [{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}];

for (var i = 0; i < obj.length; i++) {
    console.log("PAIR " + i + ": " + obj[i].oid);
    console.log("PAIR " + i + ": " + obj[i].cid);
}

Demo: http://jsfiddle.net/sTSX2/

tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • ok.. this works fine.. but how do i use the logged values??? sorry i dont know much about console.log – xprilion Aug 07 '13 at 13:23
  • 1
    @anubhavssd -- Replace console.log with whatever you want to do with them. Assign them to a variable, assign to an input, etc. Simply reference `obj[i].oid` and `obj[i]cid` – tymeJV Aug 07 '13 at 13:27
  • how to assign each 'oid' as an array of oids? like if you want array of [2, 4, 4] – Brian Wiley Dec 26 '20 at 11:39
1

This is an array that you have //lets call it a:

[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]

To get first element :

a[0] // this will give you first object i.e {"oid":"2","cid":"107"}

a[0]["oid"] // this will give you the value of the first object with the key "oid" i.e 2

and so on ...

Hope that helps. `

woofmeow
  • 2,370
  • 16
  • 13
0

Basically what you need is grouping of objects in the array with a property. Here I am giving two functions using which you can do this

// To map a property with other property in each object. 
function mapProperties(array, property1, property2) {

    var mapping = {};

    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        mapping[item[property1]] = mapping[item[property1]] || [];
        mapping[item[property1]].push(item[property2]);
    }

    return mapping;

}

// To index the items based on one property.
function indexWithProperty(array, property) {
    var indexing = {};

    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        indexing[item[property]] = indexing[item[property]] || [];
        indexing[item[property]].push(item);
    }

    return indexing;
}

var data = [{
    "oid": "2",
    "cid": "107"
}, {
    "oid": "4",
    "cid": "98"
}, {
    "oid": "4",
    "cid": "99"
}];


var oidCidMapping = mapProperties(data, "oid", "cid");
console.log(oidCidMapping["2"]); // array of cids with oid "2"

var indexedByProperty = indexWithProperty(data, "oid");
console.log(indexedByProperty["4"]); // array of objects with cid "4"

May not be the exact solution you need, but I hope I am giving you the direction in which you have to proceed.

If you are willing to use other library you can achieve the same with underscorejs

Diode
  • 24,570
  • 8
  • 40
  • 51