Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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 将对象的值从字符串转换为数字_Javascript_Reactjs_Object_Url_React Router - Fatal编程技术网

Javascript 将对象的值从字符串转换为数字

Javascript 将对象的值从字符串转换为数字,javascript,reactjs,object,url,react-router,Javascript,Reactjs,Object,Url,React Router,我当前正在尝试将此URL转换为对象: 培根=0,奶酪=0,肉=0,沙拉=1 就我所知: const urlParams = new URLSearchParams(props.match.params.ingredients); const entries = urlParams.entries(); const params = Object.fromEntries(entries); 我现在有这个: {培根:“0”,奶酪:“0”,肉:“0”,沙拉:“1”} 我需要将这些值转换成数字 我尝试

我当前正在尝试将此URL转换为对象: 培根=0,奶酪=0,肉=0,沙拉=1

就我所知:

const urlParams = new URLSearchParams(props.match.params.ingredients);
const entries = urlParams.entries();
const params = Object.fromEntries(entries);
我现在有这个: {培根:“0”,奶酪:“0”,肉:“0”,沙拉:“1”}

我需要将这些值转换成数字


我尝试过使用循环并迭代Number()和parseInt(),但我就是想不出来。

在传递到
fromEntries
之前,将每个值映射到一个数字:

const urlParams = new URLSearchParams(props.match.params.ingredients);
const entries = [...urlParams.entries()];
const entriesNumeric = entries.map(([key, value]) => ([key, Number(value)]));
const params = Object.fromEntries(entriesNumeric);

将每个值映射到一个数字,然后再从Entries传递到

const urlParams = new URLSearchParams(props.match.params.ingredients);
const entries = [...urlParams.entries()];
const entriesNumeric = entries.map(([key, value]) => ([key, Number(value)]));
const params = Object.fromEntries(entriesNumeric);

我得到这个错误:TypeError:entries.map不是一个函数。我已经尝试过很多次了。看起来像
fromEntries
可以接受任何iterable,而不仅仅是数组-只需先将
entries()
迭代器转换为数组对不起,我不确定你的意思。
entries()
返回迭代器,所以你只需要将它转换为数组。完美的非常感谢。我得到这个错误:TypeError:entries.map不是一个函数。我已经尝试过很多次了。看起来像
fromEntries
可以接受任何iterable,而不仅仅是数组-只需先将
entries()
迭代器转换为数组对不起,我不确定你的意思。
entries()
返回迭代器,所以你只需要将它转换为数组。完美的非常感谢。