Javascript 如何根据obj标志的状态查找数组长度
我有一个TODO数组,我正在尝试显示已完成TODO和未完成TODO的数量。我知道我可以做Javascript 如何根据obj标志的状态查找数组长度,javascript,arrays,reactjs,object,Javascript,Arrays,Reactjs,Object,我有一个TODO数组,我正在尝试显示已完成TODO和未完成TODO的数量。我知道我可以做array.length,我可以找到总数,但在这种情况下,我是stack this.state = { text: '', notes: [ {todo: "hello", completed: true}, {todo: "world", completed: false} ] } 全部(注释长度)2已完成(?)0未完成(?)0使用过滤器 let completedCoun
array.length
,我可以找到总数,但在这种情况下,我是stack
this.state = {
text: '',
notes: [
{todo: "hello", completed: true},
{todo: "world", completed: false}
]
}
全部(注释长度)2已完成(?)0未完成(?)0使用
过滤器
let completedCount = this.state.notes.filter(n => n.completed).length;
let incompleteCount = this.state.notes.length - completedCount;
或者使用reduce
let result = this.state.notes.reduce( (acc, curr) => {
if(curr.completed) acc.complete++;
else acc.incomplete++;
return acc;
}, {complete:0,incomplete:0});
console.log(result.complete); // 1
console.log(result.incomplete); // 1
使用过滤器
let completedCount = this.state.notes.filter(n => n.completed).length;
let incompleteCount = this.state.notes.length - completedCount;
或者使用reduce
let result = this.state.notes.reduce( (acc, curr) => {
if(curr.completed) acc.complete++;
else acc.incomplete++;
return acc;
}, {complete:0,incomplete:0});
console.log(result.complete); // 1
console.log(result.incomplete); // 1
您可以筛选已完成的笔记,然后检查长度:
const completedNotes = notes.filter((note) => note.completed)
completedNotes.length
这对您有帮助吗?您可以过滤已完成的笔记,然后检查长度:
const completedNotes = notes.filter((note) => note.completed)
completedNotes.length
这对您有帮助吗?您可以使用filter()
减少对象数组中的记录,并可以找到当前记录的长度,其行为类似于SQL查询中的where
。可以向数组中的任何键添加条件
completed=state.notes.filter(x=>x.completed==true).length;
不完整=state.notes.filter(x=>x.Completed==false).length
您可以使用filter()
减少对象数组中的记录,并可以找到存在记录的长度,其行为类似于SQL查询中的where
。可以向数组中的任何键添加条件
completed=state.notes.filter(x=>x.completed==true).length;
不完整=state.notes.filter(x=>x.Completed==false).length代码>使用reduce可以实现这一点
var a = {
text: '',
notes: [
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: true}
]
};
let count = a.notes.reduce((b,n)=>{
if (n.completed)
b.x.push(n);
else
b.y.push(n)
return b;
}, {x:[], y:[]});
console.log(count.x.length + ' ' + count.y.length);
使用reduce遍历notes数组,传递一个空对象作为reduce的参数(该对象应包含completed和incompleted作为键),从reduce返回新对象,并在该数组上应用length属性,您将获得两者的长度
jsbin:可以使用reduce实现这一点
var a = {
text: '',
notes: [
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: false},
{todo: "hello", completed: true},
{todo: "world", completed: true}
]
};
let count = a.notes.reduce((b,n)=>{
if (n.completed)
b.x.push(n);
else
b.y.push(n)
return b;
}, {x:[], y:[]});
console.log(count.x.length + ' ' + count.y.length);
使用reduce遍历notes数组,传递一个空对象作为reduce的参数(该对象应包含completed和incompleted作为键),从reduce返回新对象,并在该数组上应用length属性,您将获得两者的长度
jsbin: