[英]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! 非常感谢您的帮助,谢谢!
.each
as there is no nested array. .each
因为没有嵌套数组。 callback
(A function to execute for each element) is the item iterated in the current loop, not the first argument. callback
第二个参数(为每个元素执行的函数)是当前循环中迭代的项,而不是第一个参数。 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.