为什么'onClick = onClickRemove(items.objectID)'给出了未定义的错误项: 数组列表:
const list = [
{
title: 'React',
author: 'Jordan Walke',
objectID: 0,
},
{
title: 'Redux',
author: 'Dan Abramov, Andrew Clark',
objectID: 1,
},
]
map的提取功能:这里onClickremove(items.objectID)没有从items数组中获取属性?为什么请解释。
function extractProps(items){
var x = "<div> <span>" +items.title+", </span> <span>"+items.author +
"</span><span>
<button type='button' onClick='onClickRemove(items.objectID)'>Remove</button></span></div> "
return x;
}
“onClickRemove”功能:
function onClickRemove(id){
console.log(id)
// const updatedList = list.filter(isNotId = (item)=> item.objectID!==id);
// console.log(id);
// mapFilteredArray(updatedList);
}
主要功能:
function myFunc(searchitem=''){
document.getElementById('root').innerHTML = "";
var y= list.filter( (item) => item.title.toLowerCase().includes(searchitem.toLowerCase())).map(extractProps).join("");;
document.getElementById('root').innerHTML = filteredList;
}
myFunc();
答案 0 :(得分:0)
看看这个:
const list = [
{ title: 'React',author: 'Jordan Walke',objectID: 0,},
{ title: 'Redux',author: 'Dan Abramov, Andrew Clark',objectID: 1,}
]
function extractProps(items){
var x = "<div> <span>" +items.title+", </span> <span>"+items.author +
"</span><span><button type='button' onClick='onClickRemove("+items.objectID+")'>Remove</button></span></div> "
return x;
}
function onClickRemove(id){
console.log(id)
// const updatedList = list.filter(isNotId = (item)=> item.objectID!==id);
// console.log(id);
// mapFilteredArray(updatedList);
}
function myFunc(searchitem=''){
document.getElementById('root').innerHTML = "";
var filteredList= list.filter( (item) => item.title.toLowerCase().includes(searchitem.toLowerCase())).map(extractProps).join("");;
document.getElementById('root').innerHTML = filteredList;
}
myFunc();
<div id="root"></div>
您将items.objectID
作为字符串插入此处'onClickRemove(items.objectID)'
,从而导致问题。我提取它并连接它的实际值