簡體   English   中英

獲取特定的數組值javascript

[英]Get specific array value javascript

我不確定自己是否很傻,雖然我無法在任何地方找到它,但是我正在json_encode()一些數據庫輸出並將其輸出到頁面,並使用$.parseJSON讀取,但是我想從數組中獲取一個特定的值。 這是我要完成的示例。

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(或name或profile_img,通常是一個值),以便稍后將這些值插入div中。

非常感謝您的幫助,謝謝!

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

each級別都不需要第二級。

嘗試這個:

 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> 

嘗試這個 :-

僅使用JS:

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

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

將您的json對象解析為數組列表后,您可以使用如下所示的vanilla map函數

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

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

這可能對您有幫助。

   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

不必使用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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM