Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays_Object_Local Storage_Push - Fatal编程技术网

Javascript 将对象推入对象数组会复制该数组中已存在的对象

Javascript 将对象推入对象数组会复制该数组中已存在的对象,javascript,arrays,object,local-storage,push,Javascript,Arrays,Object,Local Storage,Push,正如问题所说,当我将对象推入一个对象数组时,它会复制该数组中已经存在的对象。只有在重新加载页面后,副本才会被删除。我知道这和参考资料有关。我试着复制被推到阵列中的对象,创建了一个带有道具的新对象。任何帮助都将不胜感激。谢谢 // The persons array is an array of objects import { persons } from './main.js'; let entriesFound = [] let data = localStorage.getItem('

正如问题所说,当我将对象推入一个对象数组时,它会复制该数组中已经存在的对象。只有在重新加载页面后,副本才会被删除。我知道这和参考资料有关。我试着复制被推到阵列中的对象,创建了一个带有道具的新对象。任何帮助都将不胜感激。谢谢

// The persons array is an array of objects 
import { persons } from './main.js';

let entriesFound = []
let data = localStorage.getItem('entriesFound') ;

if(data){
    entriesFound = JSON.parse(data)
    loadList(entriesFound)
}

//Renders the search results to the UI from localStorage
function loadList(array) {

    for(let el of array) {
        const html = 
        `<div id="${el.id}" class="item2">
        <div class="info2">Name:<div class="name">${el.name}</div></div>
        <div class="info2">Date of birth:<div class="born">${el.dob}</div></div>
        <div class="info2">Age:<div class="age">${el.age}</div></div>
        <div class="info2">Place of birth:<div class="city">${el.city}</div></div>
        <div class="info2">ID:<div class="id">${el.id}</div></div>
        <div class="info2">Entered:<div class="added">${el.entered}</div></div>
        </div>`;

     document.querySelector('.searchResult').insertAdjacentHTML('beforeend', html)
    }
}

//Search button to search for entry (in the persons array) that matches the condtional 
export const searchBtn = document.querySelector('.search').addEventListener('click' , function() {

    // Get search string from search bar
    const name = document.querySelector('.searchInput')

   // persons array
     persons.filter( el => {
        if(el.name === name.value) {
           entriesFound.push(el);   // Pushes the object (el) to the entriesFound array
        }                           // I guess this is were it goes wrong
    })
        addItem(entriesFound)
        name.value = ""
        localStorage.setItem('entriesFound', JSON.stringify(entriesFound))
})

// Renders the new search result to the UI
function addItem(entries) {
    for( let item of entries) {
        const html = 
                `<div id="${item.id}" class="item2">
                <div class="info2">Name:<div class="name">${item.name}</div></div>
                <div class="info2">Date of birth:<div class="born">${item.dob}</div></div>
                <div class="info2">Age:<div class="age">${item.age}</div></div>
                <div class="info2">Place of birth:<div class="city">${item.city}</div></div>
                <div class="info2">ID:<div class="id">${item.id}</div></div>
                <div class="info2">Entered:<div class="added">${item.entered}</div></div>
                </div>`;
    
             document.querySelector('.searchResult').insertAdjacentHTML('beforeend', html)
        }
}
//persons数组是一个对象数组
从'/main.js'导入{persons};
让entriesFound=[]
让data=localStorage.getItem('entriesFound');
如果(数据){
entriesFound=JSON.parse(数据)
加载列表(entriesFound)
}
//从localStorage将搜索结果呈现到UI
函数加载列表(数组){
for(让el表示数组){
常量html=
`
名称:${el.Name}
出生日期:${el.dob}
年龄:${el.Age}
出生地:${el.city}
ID:${el.ID}
输入:${el.Entered}
`;
document.querySelector('.searchResult').insertAdjacentHTML('beforeend',html)
}
}
//搜索按钮,用于搜索与条件匹配的条目(在人员数组中)
export const searchBtn=document.querySelector('.search').addEventListener('click',function()){
//从搜索栏获取搜索字符串
const name=document.querySelector(“.searchInput”)
//人员数组
persons.filter(el=>{
如果(el.name==name.value){
entriesFound.push(el);//将对象(el)推送到entriesFound数组
}//我想这是因为它出了问题
})
附加项(entriesFound)
name.value=“”
localStorage.setItem('entriesFound',JSON.stringify(entriesFound))
})
//将新的搜索结果呈现到UI
函数附加项(条目){
对于(让条目项){
常量html=
`
名称:${item.Name}
出生日期:${item.dob}
年龄:${item.Age}
出生地:${item.city}
ID:${item.ID}
已输入:${item.Entered}
`;
document.querySelector('.searchResult').insertAdjacentHTML('beforeend',html)
}
}

找到了问题。addItem函数在整个entriesFound数组中循环,而它只需向搜索结果中添加一个条目

     persons.forEach( el => {
        if(el.name === name.value) {
           addItem(el)                 <---- like so, el is an object from 
                                             the persons array of objects!
           entriesFound.push(el);   
        }                           
    })
        addItem(entriesFound)
        name.value = ""
        localStorage.setItem('entriesFound', JSON.stringify(entriesFound))
})

// Renders the new search result to the UI
function addItem(entry) {
                                       <--- got rid of the loop!
        const html = 
                `<div id="${entry.id}" class="item2">
                <div class="info2">Name:<div class="name">${entry.name}</div></div>
                <div class="info2">Date of birth:<div class="born">${entry.dob}</div></div>
                <div class="info2">Age:<div class="age">${entry.age}</div></div>
                <div class="info2">Place of birth:<div class="city">${entry.city}</div></div>
                <div class="info2">ID:<div class="id">${entry.id}</div></div>
                <div class="info2">Entered:<div class="added">${entry.entered}</div></div>
                </div>`;
    
             document.querySelector('.searchResult').insertAdjacentHTML('beforeend', html)
        }
persons.forEach(el=>{
如果(el.name==name.value){

addItem(el)而不是
persons.filter()
使用
persons.forEach()
。filter()方法创建一个新数组,其中包含通过测试的所有元素,在本例中不需要实现。是的,我知道,我确实做到了,但这并没有解决我在该阶段遇到的问题。无论如何,感谢您的检查。