gpt4 book ai didi

jQuery:识别声明变量的值与数组中键列表中的值相同

转载 作者:行者123 更新时间:2023-12-01 04:43:37 25 4
gpt4 key购买 nike

我有一组 json 数组,如下所示:

{"status":200,"success":true,"result":
[{"id":"0","name":"user1","type":"mf","message":"bonjour user1"},
{"id":"1","name":"user2","type":"ff","message":"hello user2"},
{"id":"2","name":"user3","type":"mm","message":"konnichiwa user3"},
{"id":"3","name":"user4","type":"mf","message":"ni hao user4"},
{"id":"4","name":"user5","type":"ff","message":"high 5! user5"}]}

我想问如何确定已声明变量的值(例如,值为“user1”的“getName”)与数组中“name”键列表中的值是否相同?

下面是我的尝试,您可能会找到我的完整代码 here :

var apiUrl = "https://sheetsu.com/apis/f924526c"; 

$.getJSON(apiUrl,function(json){

console.log(JSON.stringify(json));

var getName = $.each(getName, function(content, json){
console.log(json.name);
console.log(getName[content].id);
});

var identifiedId = $.each(getName, function(){
console.log(this.name);
console.log(this.id);
})
});

最佳答案

我不确定我是否理解您的代码,但为了循环 json 并找到您想要使用的键值:

$.each(json.result, function() {
//here you can store id.
$.each(this, function(key, value) {
console.log(key + ' ' + value);
//test if key = username you want and store value
});
});

您也可以通过一个循环来完成此操作,因为您知道要检查哪个键

$.each(json.result, function() {
if (this.name == "name you look for"){
idyouneed = this.id;
}
}

关于jQuery:识别声明变量的值与数组中键列表中的值相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com