Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 jQuery从json获取toal_Javascript_Jquery_Arrays_Json - Fatal编程技术网

Javascript jQuery从json获取toal

Javascript jQuery从json获取toal,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我有一个Json,有很多页面,页面上有很多图片。 默认值为24页,如果用户输入一个数字,如56页,我如何创建一个循环,将56页中的图片总数相加。如“第1页+第2页+第3页…”,循环应自行重新绘制,直到达到56页,谢谢 [{pagenumber: “1”, numofpic: "2"}, {pagenumber: “2”, numofpic: "4"}, {pagenumber: “3”, numofpic: "1"}, {pagenumber: “4”, numofpic: "5"}

我有一个Json,有很多页面,页面上有很多图片。 默认值为24页,如果用户输入一个数字,如56页,我如何创建一个循环,将56页中的图片总数相加。如“第1页+第2页+第3页…”,循环应自行重新绘制,直到达到56页,谢谢

[{pagenumber: “1”, numofpic: "2"}, 
 {pagenumber: “2”, numofpic: "4"}, 
 {pagenumber: “3”, numofpic: "1"}, 
 {pagenumber: “4”, numofpic: "5"}, 
 {pagenumber: “5”, numofpic: "7"}]

这是你可以做到的-

let pages=[{pagenumber:“1”,numofpic:“2”},
{页码:“2”,numofpic:“4”},
{页码:“3”,numofpic:“1”},
{页码:“4”,numofpic:“5”},
{页码:“5”,numofpic:“7”}];
让userSelectedPageNumber=4;
让totalNumOfPic=pages.filter(p=>Number(p.pagenumber)Number(p.numofpic)).reduce(函数(acc,val){return acc+val;});
console.log(totalNumOfPic)
您可以使用经典方法简单地添加
设arr=[{pagenumber:“1”,numofpic:“2”},
{页码:“2”,numofpic:“4”},
{页码:“3”,numofpic:“1”},
{页码:“4”,numofpic:“5”},
{页码:“5”,numofpic:“7”}
ect…]
设结果=0;

对于(让i=0;i请展示您尝试过的内容。Stackoverflow不是免费的代码编写服务或“如何”教程服务。当您发布的代码未按预期工作时,人们将乐于帮助您。网上有很多教程可以帮助您入门
You can simply add by using the classic method

let arr= [{pagenumber: “1”, numofpic: "2"}, 
 {pagenumber: “2”, numofpic: "4"}, 
 {pagenumber: “3”, numofpic: "1"}, 
 {pagenumber: “4”, numofpic: "5"}, 
 {pagenumber: “5”, numofpic: "7"}
 ect...]

 let  result=0;

for(let i=0; i<arr.length && arr.length<=enteredNo; i++){
    result += arr[i].b;
}

Or if you want the sum of the whole array you can use

    arr.reduce((a,b)=>{return a.numofpic + b.numofpic});