简体   繁体   English

获取特定的数组值javascript

[英]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. 我不确定自己是否很傻,虽然我无法在任何地方找到它,但是我正在json_encode()一些数据库输出并将其输出到页面,并使用$.parseJSON读取,但是我想从数组中获取一个特定的值。 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. 现在,我要完成的工作就是从此数组中提取UID(或name或profile_img,通常是一个值),以便稍后将这些值插入div中。

Any help is much appreciated, thank 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); $(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. 我会避免使用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); 

No 2nd level of each is required. each级别都不需要第二级。

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> 

try this :- 尝试这个 :-

using JS only: 仅使用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'])
});

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

After parsing your json object to an array list you can just use vanilla map function as below 将您的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);
}

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

Not necessaray use Jquery! 不必使用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