2016-05-31 6 views
0

私はちょっとばかげているのかどうかわかりませんが、どこでも見つけることができませんでしたが、私はjson_encode()いくつかのデータベース出力を出してページに出力し、 $.parseJSONを使用していますが、配列から特定の値を取得します。ここに私が達成しようとしているものの例があります。特定の配列の値を取得するjavacript

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 
}); 

は今、私が達成したいものを言葉にすることはちょうどこの配列からUID(または名前やprofile_img、一般的には値だけを)引っ張っているので、私は、後に値を挿入することができますdiv。

ご協力いただきありがとうございます。ネストされた配列が存在しないとして、ネストされた.eachを使用する

+0

実行してみましたか?間違いましたか?あなたはどこにいるのですか? – gravityplanx

答えて

3

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>

私はこの操作のためjQueryを避けているだろう。

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);

0

eachのいかなる2レベルが必要とされません。

はこれを試してみてください。

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) { 
 
\t console.log(obj.uid); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1

はこれを試してください: -

JSのみ使用:jQueryを使って

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']) 
}); 

: -

0

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']); 
}); 
あなただけ

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

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

以下のようにバニラのマップ機能を使用することができ、アレイのリストにあなたのJSONオブジェクトを解析した後これは、あなたの助けになることがあります。

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 
0

不要な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] 
関連する問題