简体   繁体   English

将多个对象数组合并为一个对象数组

[英]merge multiple arrays of objects into one array of objects

I need your help.我需要你的帮助。 I have the following line of code which returns me one or more arrays depending on the checkbox that is clicked.我有以下代码行,它根据单击的复选框返回一个或多个数组。

So far everything ok.到目前为止一切正常。

selected.forEach(langsel => {
                let filtered = person.filter(pers => pers.language == langsel);
            })

selected and I do not report the other variables for simplicity in reading.选择,我不报告其他变量,以方便阅读。

For example I get:例如我得到:

First array: [{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];第一个数组:[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];

Second array: = [{id: "15", name: "Alex Atres", language: "Spanish"}, {id: "1", name: "Mark Sertoj", language: "Spanish"}, id: "12", name: "Martha Forrest", language: "Spanish"];第二个数组:= [{id: "15", name: "Alex Atres", language: "Spanish"}, {id: "1", name: "Mark Sertoj", language: "Spanish"}, id: " 12”,姓名:“玛莎福雷斯特”,语言:“西班牙语”];

These are two separate arrays;这是两个独立的数组; in the sense that if I click the checkbox of interest I get the first array, if I click a second checkbox (always leaving the first checkbox checked) I get the array related to the second checkbox losing the first related to the previously clicked checkbox.从某种意义上说,如果我单击感兴趣的复选框,我会得到第一个数组,如果我单击第二个复选框(始终选中第一个复选框),我会得到与第二个复选框相关的数组,而丢失与先前单击的复选框相关的第一个复选框。

I don't want the former to be lost but I want them to be merged into one array .我不希望前者丢失,但我希望将它们合并到一个数组中。

If you simply want to merge them and aren't worried about collisions, I would use concat.如果您只是想合并它们并且不担心碰撞,我会使用 concat。

array1.concat(array2) will solve your problem. array1.concat(array2)将解决您的问题。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

If you want some logic on the merge, there are many other ways to do it, but the best approach most likely depends on what the logic is.如果你想要一些关于合并的逻辑,还有很多其他的方法可以做到,但最好的方法很可能取决于逻辑是什么。

You can simply achieve this requirement by using Array.filter() along with Array.includes() method.您可以通过使用Array.filter()Array.includes()方法来简单地实现此要求。 I added the descriptive comments in the below code snippet.我在下面的代码片段中添加了描述性注释。

Try this :试试这个

 // Input person array const person = [{ id: "2", name: "Tomas Addreh", language: "English" }, { id: "6", name: "Mark Addreh", language: "English" }, { id: "15", name: "Alex Atres", language: "Spanish" }, { id: "1", name: "Mark Sertoj", language: "Spanish" }, { id: "12", name: "Martha Forrest", language: "Spanish" }]; // Initializing an array to get selected checkbox values. const selectedPerson = []; // getSelectedPerson() method invoke on checkbox value change. function getSelectedPerson(event) { // This line of code is used to push the selected languages from the checkboxes on checked into an array and remove if checbox unchecked. event.target.checked ? selectedPerson.push(event.target.value) : selectedPerson.splice(selectedPerson.indexOf(event.target.value), 1); // If there is any checbox selected then it will go inside this condition. if (selectedPerson.length) { // To filtered out the person array based on the languages available in selectedPerson array. const filtered = person.filter(({ language }) => selectedPerson.includes(language)); // Assignign a result into a "result" div document.getElementById('result').innerHTML = JSON.stringify(filtered, null, 2); } else { // else case document.getElementById('result').innerHTML = []; } }
 <input type="checkbox" id="english" name="english" value="English" onchange="getSelectedPerson(event)"> <label for="english"> English</label><br> <input type="checkbox" id="spanish" name="spanish" value="Spanish" onchange="getSelectedPerson(event)"> <label for="spanish"> Spanish</label><br> <pre id="result"></pre>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM