从json数组中迭代并输出条件数据
Iterate and ouput conditional data from a json array
我有一个名为steps.json的json文件,其中包含这个json数组,我想根据下面的条件对其进行迭代。
{
"friends": [
{ "firstName" : "Paul", "lastName" : "Taylor", "Step": 2 },
{ "firstName" : "Sharon", "lastName" : "Thomas", "Step": 3 },
{ "firstName" : "Thomas", "lastName" : "Harris", "Step": 3 },
{ "firstName" : "Deborah", "lastName" : "Lee", "Step": 4 },
{ "firstName" : "Mark", "lastName" : "Young", "Step": 4 },
{ "firstName" : "Shirley", "lastName" : "Perez", "Step": 4 },
{ "firstName" : "Joseph", "lastName" : "Lee", "Step": 5 },
{ "firstName" : "Mary", "lastName" : "White", "Step": 5 },
{ "firstName" : "Matthew", "lastName" : "Garcia", "Step": 5 },
{ "firstName" : "Patricia", "lastName" : "Allen", "Step": 5 },
{ "firstName" : "Larry", "lastName" : "Robinson", "Step": 6 },
{ "firstName" : "Kimberly", "lastName" : "Lopez", "Step": 6 },
{ "firstName" : "Jose", "lastName" : "Martinez", "Step": 6 },
{ "firstName" : "Deborah", "lastName" : "Walker", "Step": 6 },
{ "firstName" : "Joseph", "lastName" : "Lopez", "Step": 6 },
{ "firstName" : "Dorothy", "lastName" : "Moore", "Step": 7 },
{ "firstName" : "Jose", "lastName" : "Jackson", "Step": 7 },
{ "firstName" : "Karen", "lastName" : "Lee", "Step": 7 },
{ "firstName" : "Paul", "lastName" : "Taylor", "Step": 7 },
{ "firstName" : "Amy", "lastName" : "Gonzalez", "Step": 7 },
{ "firstName" : "Richard", "lastName" : "Martinez", "Step": 7 }
]
}
当用户单击与步骤号匹配的按钮时,我试图输出数组中的对象。例如,如果用户单击按钮2,我想显示步骤2中的所有朋友,以此类推。我得到了这样的json,但不知道如何设置点击请求。
findFriends :function(){
var urlString = 'assets/javascripts/steps.json';
$.getJSON(urlString,function(data){
var friends = data.friends;
for(var i = 0; i <= friends.length; i++){
for(friend in friends[i]){
}
}
});
}
下面是工作代码。点击下面的按钮运行它。
$(function() {
var friends = [{
"firstName": "Paul",
"lastName": "Taylor",
"Step": 2
}, {
"firstName": "Sharon",
"lastName": "Thomas",
"Step": 3
}, {
"firstName": "Thomas",
"lastName": "Harris",
"Step": 3
}, {
"firstName": "Deborah",
"lastName": "Lee",
"Step": 4
}, {
"firstName": "Mark",
"lastName": "Young",
"Step": 4
}, {
"firstName": "Shirley",
"lastName": "Perez",
"Step": 4
}, {
"firstName": "Joseph",
"lastName": "Lee",
"Step": 5
}, {
"firstName": "Mary",
"lastName": "White",
"Step": 5
}, {
"firstName": "Matthew",
"lastName": "Garcia",
"Step": 5
}, {
"firstName": "Patricia",
"lastName": "Allen",
"Step": 5
}, {
"firstName": "Larry",
"lastName": "Robinson",
"Step": 6
}, {
"firstName": "Kimberly",
"lastName": "Lopez",
"Step": 6
}, {
"firstName": "Jose",
"lastName": "Martinez",
"Step": 6
}, {
"firstName": "Deborah",
"lastName": "Walker",
"Step": 6
}, {
"firstName": "Joseph",
"lastName": "Lopez",
"Step": 6
}, {
"firstName": "Dorothy",
"lastName": "Moore",
"Step": 7
}, {
"firstName": "Jose",
"lastName": "Jackson",
"Step": 7
}, {
"firstName": "Karen",
"lastName": "Lee",
"Step": 7
}, {
"firstName": "Paul",
"lastName": "Taylor",
"Step": 7
}, {
"firstName": "Amy",
"lastName": "Gonzalez",
"Step": 7
}, {
"firstName": "Richard",
"lastName": "Martinez",
"Step": 7
}];
function findFriends(step) {
var urlString = 'assets/javascripts/steps.json';
// commenting out the ajax lines for the purposes of this demo. for now we'll use the global friends variable declared above
// $.getJSON(urlString, function(data) {
// var friends = data.friends;
var results = '';
var numFound = 0;
$.each(friends, function(key, value) {
if (value.Step == step) {
numFound++;
console.log (numFound);
if (numFound <= 2) {
results += value.firstName + ' ' + value.lastName + '<br>';
}
};
});
if (numFound > 2) {
results += 'and ' + (numFound - 2) +
' other friend' + (numFound == 3 ? '' : 's');
}
$('#results').html(results);
//});
}
$('#find-friends').click(function() {
// note the plus to convert the string into a number
findFriends(+$('#step').val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Enter the step you want to test:
<br>
<input type="text" id="step">
<br>
<button id="find-friends">click me!</button>
<div id="results"></div>
您可以为此使用[].filter()
。
friends.filter(function(f){return f.step == 2});
// friend finder function
function findFriends(stepNo) {
var urlString = 'assets/javascripts/steps.json';
$.getJSON(urlString, function(data){
var friends = data.friends.filter(function(friend) {
return friend.Step === stepNo;
});
// do something with the found friends :) (e.g. fill a list)
});
}
// click handler
$('.yourButtonSelector').click(function() {
var stepNo = $(this).attr('data-stepNo'); // not sure how you store the step no. in the buttons
findFriends(stepNo);
});
附带说明:如果您可以控制JSON格式,并且主要针对所描述的格式使用此服务,我建议使用stepNo
作为密钥:
{
friends: {
1: [...],
2: [...],
...
}
}
这里num是动态值,您可以传递它,并根据每个索引过滤
var friends = data.friends;
var num = 6;
var filteredNames = $(friends).filter(function( idx ) {
return friends[idx].Step === num;
});
相关文章:
- 在JavaScript中输出转义字符
- 通过js在新选项卡中有条件地打开url
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- jQuery-有条件地附加HTML
- 根据某些条件在视图之间切换
- 如何做到这一点,使代码在不传递条件后执行函数
- JavaScript IF.ELSE条件未输出预期值
- 淘汰赛:can't在foreach块内进行条件输出
- 如果 java 脚本中的其他条件,则无法正确输出
- 在MVC3中;关闭条件编译“;将Model变量输出到JavaScript时发出警告
- 从json数组中迭代并输出条件数据
- 遍历javascript对象并根据条件输出某些信息
- 如何循环使用条件遍历JavaScript对象,然后输出数据
- Javascript:在条件中比较函数和数组的输出
- 返回错误输出的Javascript条件语句
- 余烬CLI-在每个循环中使用组件而不是itemController的条件输出
- jQuery AJAX在第一个PHP条件之后不显示输出
- 使用javascript有条件地格式化php输出
- Javascript高级&较低的变量条件奇数控制台输出
- 复杂的if语句运行代码而不考虑条件输出