Animadder
Animadder

Reputation: 1

Get specific array value javascript

I'm not sure if I'm just being silly, although I wasn't able to find this anywhere, but I am json_encode()ing some database outputs and outputting them to the page, and reading them using $.parseJSON, but I want to get a specific value from the array. Here is an example of what I'm trying to accomplish.

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  $.each(val, function(k, v) {
    console.log(k['uid']) // <-- This is where I want to just output the UID from each array results
});

Now, to put into words what I'd like to accomplish is to just pull the UID (or name or profile_img, just a value in general) from this array, so I can later insert the values into a div.

Any help is much appreciated, thank you!

Upvotes: 0

Views: 97

Answers (6)

Jules Goullee
Jules Goullee

Reputation: 591

Not necessaray use Jquery!

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
var parsedJson = JSON.parse(j);
var uidsArray = parsedJson.map( function(entry){
   return entry.uid;
});

console.log(uidsArray); // output: [1,2]

Upvotes: 0

Satyapriya Mishra
Satyapriya Mishra

Reputation: 130

This may be of help to you.

   var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

        var json = $.parseJSON(j);
        var l=json.length;
        var arr=[];
        for(var i=0;i<l;i++){
            arr.push(json[i]['uid']);
        }
        console.log(arr);//you can use this array for whatever purpose you intend to

Upvotes: 0

Rayon
Rayon

Reputation: 36609

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  console.log(val['uid']);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

I would have avoided jQuery for this manipulation.

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
var parsed = JSON.parse(j);
var uids = parsed.map(function(item) {
  return item.uid;
});
console.log(uids);

Upvotes: 3

Ashish Kumar
Ashish Kumar

Reputation: 3039

No 2nd level of each is required.

try this:

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$.each(json, function (i, obj) {
	console.log(obj.uid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Atul Chaudhary
Atul Chaudhary

Reputation: 3736

After parsing your json object to an array list you can just use vanilla map function as below

function stuff(item,index) {
    //Here can do any manipulation with your object
    return item.uid;
}

function myFunction() {
    document.getElementById("myDiv").innerHTML = myArrayList.map(stuff);
}

Upvotes: 0

Sabith
Sabith

Reputation: 1656

try this :-

using JS only:

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
j = JSON.parse(j);
j.map((value)=>{
    console.log(value['uid'])
});

With jQuery:-

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  console.log(val['uid']);
});

Upvotes: 0

Related Questions