Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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 如何将获取值传递到json中?_Javascript_Json_Fetch - Fatal编程技术网

Javascript 如何将获取值传递到json中?

Javascript 如何将获取值传递到json中?,javascript,json,fetch,Javascript,Json,Fetch,我想将value变量从one()函数传递到x json。x json不应与one()函数或任何其他函数一起使用。可能吗 function something(){ 功能一(){ 让url为空https://data.heroku.com/dataclips/szmszzhrbbuhxqfunbmblylxijko.json'; 获取(url) .then(res=>res.json()) .然后((out)=>out.values[out.values.length-1][2]) 。然后((值

我想将value变量从one()函数传递到x json。x json不应与one()函数或任何其他函数一起使用。可能吗

function something(){
功能一(){
让url为空https://data.heroku.com/dataclips/szmszzhrbbuhxqfunbmblylxijko.json';
获取(url)
.then(res=>res.json())
.然后((out)=>out.values[out.values.length-1][2])
。然后((值s2)=>乘以(值s2))
.catch((err)=>{throw err});
函数乘法(值){
var金额=document.getElementById(“金额”).value;
var金额2=(值*金额);
常量值=amounts2.toString();
console.log(值);
返回值;//我需要从这里得到这个变量#####
}
}
一个();
var x={购买单位:[
{
金额:{
值:值,到这里#####
},
},
],
};
log(JSON.stringify(x));
}

输入金额:

提交
您可以从one()函数返回JSON值,如下所示:

function one() {
    let returnedValue = null

    let url = 'https://data.heroku.com/dataclips/szmszzhrbbuhxqfunbmblylxijko.json';
    fetch(url)
    .then(res => { returnedValue = res.json() })

    return returnedValue
}

const x = one()

谢谢你,但是我怎样才能得到一个()来返回乘法(values)的值呢?有人这样想:
。然后(res=>{returnedValue=multiply(res.json())}
我尝试了
。然后((values2)=>returnedValue=multiply(values2))
并且它将returnedValue显示为null。看一看:我用async/wait更新了我的解决方案。这不完全是我想要的,但你帮了我,谢谢。