nb_*_*_nb 1 javascript arrays jquery json javascript-objects
我有一个 JavaScript.The 对象看起来像
{date: "2019-10-03", hello: 0, yo: 0, test: 0}
Run Code Online (Sandbox Code Playgroud)
我可以检查对象中的所有值是否==0都不是日期吗?
我不确定如何进行编码。
使用解构提取日期和性质的休息和计算sum的的Object.values使用Array.reduce:
const obj = { date: "2019-10-03", hello: 0, yo: 0, test: 0 };
const { date, ...rest } = obj;
const sum = Object.values(rest).reduce((sum, curr) => sum + curr, 0);
const allZeros = sum === 0 ? true : false;
console.log(allZeros);Run Code Online (Sandbox Code Playgroud)
(请记住,这将date在当前范围内创建一个变量)
或者,使用Array.every
const obj = { date: "2019-10-03", hello: 0, yo: 0, test: 0 };
const { date, ...rest } = obj;
const allZeros = Object.values(rest).every(e => e === 0);
console.log(allZeros);Run Code Online (Sandbox Code Playgroud)