Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取特定数组值javascript_Javascript - Fatal编程技术网

获取特定数组值javascript

获取特定数组值javascript,javascript,Javascript,我不确定我是否只是在傻,虽然我在任何地方都找不到这个,但我正在json_encode()读取一些数据库输出并将它们输出到页面,然后使用$.parseJSON读取它们,但我想从数组中获得一个特定的值。这是一个我正在努力完成的例子 var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]'

我不确定我是否只是在傻,虽然我在任何地方都找不到这个,但我正在
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
});
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(函数(i,val){
$。每个(val,函数(k,v){
console.log(k['uid'])/
  • 无需使用嵌套的
    。每个
    ,因为没有嵌套数组
  • 中的第二个参数是在当前循环中迭代的项,而不是第一个参数
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(函数(i,val){
日志(val['uid']);
});
  • 无需使用嵌套的
    。每个
    ,因为没有嵌套数组
  • 中的第二个参数是在当前循环中迭代的项,而不是第一个参数
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(函数(i,val){
日志(val['uid']);
});

每个
都不需要第二级

试试这个:

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,函数(i,obj){
控制台日志(obj.uid);
});

每个
都不需要第二级

试试这个:

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,函数(i,obj){
控制台日志(obj.uid);
});
试试这个:-

仅使用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']);
});
试试这个:-

仅使用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对象解析为数组列表后,您只需使用下面的香草映射函数

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

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

在将json对象解析为数组列表后,您只需使用下面的香草映射函数

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
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=[];

对于(var i=0;i这可能对您有所帮助

   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
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=[];

对于(var i=0;i不必使用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]

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

你试过运行它吗?你出错了吗?你被卡在哪里了?你试过运行它吗?你出错了吗?你被卡在哪里了?