ผสานอาร์เรย์หนึ่งอาร์เรย์หลังจากการกรอง


14

ฉันมีอาเรย์ของวัตถุที่ฉันใช้เฉพาะอาเรย์ตำแหน่ง เป้าหมายของฉันคือการรวมอาร์เรย์ตำแหน่งที่ตั้งเหล่านี้เข้ากับหนึ่งอาร์เรย์อย่างไรก็ตามฉันไม่สามารถทำเช่นนั้นและรับอาร์เรย์ว่าง นี่คือวิธีที่ฉันทำ:

let results = [{
    id: '1',
    locations: ['aaaa', 'bbbbbb', 'cccccc']
  },
  {
    id: '2',
    locations: []
  },
  {
    id: '3',
    locations: ['ddd', 'aaadsad', 'sefd']
  },
  {
    id: '4',
    locations: ['ffff', 'eeee', 'sfdsfsd']
  },
];
const locationIds = [].concat.apply([], ...results.filter(s => s.locations && s.locations.length > 0).map(({
  locations
}) => ({
  locations
})));

console.log(locationIds);

ฉันทำอะไรผิดที่นี่ ผลควรเป็น ['aaaa', 'bbbbbb', 'cccccc', 'ddd', 'aaadsad', 'sefd', 'ffff', 'eeee', 'sfdsfsd'];

คำตอบ:


9

คุณไม่ต้องการfilterที่นี่ เพียงแค่ใช้mapวิธีการโดยผ่านการเรียกกลับฟังก์ชั่นที่มีให้ซึ่งจะใช้กับทุกรายการในอาร์เรย์

let results = [{ id: '1', locations: ['aaaa', 'bbbbbb', 'cccccc'] }, { id: '2', locations: [] }, { id: '3', locations: ['ddd', 'aaadsad', 'sefd'] }, { id: '4', locations: ['ffff', 'eeee', 'sfdsfsd'] }, ];

const locationIds = [].concat(...results.map(s => s.locations));

console.log(locationIds);


1
@ user122222 ยินดีต้อนรับ! คุณสามารถสร้างสิ่งที่คุณflatMapชอบได้ที่นี่: stackoverflow.com/questions/39837678/…
Mihai Alexandru-Ionut

7

คุณสามารถลองด้วยflatMap():

flatMap()วิธีแรกแผนที่แต่ละองค์ประกอบโดยใช้ฟังก์ชั่นการทำแผนที่แล้ว flattens ผลเป็นอาร์เรย์ใหม่ มันเป็นเหมือนกันไปmap()ตามด้วยflat()ของความลึก 1แต่flatMap()มักจะเป็นประโยชน์อย่างมากในขณะที่การควบรวมกิจการทั้งสองไว้ในวิธีการหนึ่งคือการออกไปเล็กน้อยมีประสิทธิภาพมากขึ้น

let results = [{
    id: '1',
    locations: ['aaaa', 'bbbbbb', 'cccccc']
  },
  {
    id: '2',
    locations: []
  },
  {
    id: '3',
    locations: ['ddd', 'aaadsad', 'sefd']
  },
  {
    id: '4',
    locations: ['ffff', 'eeee', 'sfdsfsd']
  },
];
const locationIds = results.flatMap(i => i.locations);
console.log(locationIds);


6

คุณสามารถทากาArray#flatMapกับทรัพย์สินที่ต้องการได้ || []ถ้าคุณสมบัติไม่ได้รับเพิ่มอาร์เรย์เริ่มต้น

let results = [{ id: '1', locations: ['aaaa', 'bbbbbb', 'cccccc'] }, { id: '2', locations: [] }, { id: '3', locations: ['ddd', 'aaadsad', 'sefd'] }, { id: '4', locations: ['ffff', 'eeee', 'sfdsfsd'] }],
    locationIds = results.flatMap(({ locations }) => locations);

console.log(locationIds);
.as-console-wrapper { max-height: 100% !important; top: 0; }


2
เป็นเรื่องที่ดีที่จะพูดถึง. flatMap ไม่สามารถใช้งานใน Edge และ IE ได้หากไม่มี polyfills
Zydnar

3

สามารถแก้ปัญหาโดยใช้ฟังก์ชั่นการลดจาก Array.prototype

var newResults = results.reduce(function(acc, curr) {
    return acc.concat(curr.locations)
  },[]
)

หวังว่านี่จะช่วยได้


2

ฉันใช้เวลานานเกินไปในการไม่โพสต์คำตอบของตัวเอง - ตัวต่อที่น่าสนใจสำหรับฉันแม้ว่าคำตอบอื่น ๆ นั้นไม่ต้องสงสัยเลยว่าสามารถแสดงได้ มันใช้กลยุทธ์ประเภทเดียวกันกับโพสต์ดั้งเดิมของคุณซึ่งอาจช่วยชี้ให้เห็นว่ามันเกิดความผิดพลาดที่ไหน

const locationIds = [].concat
                    .apply([], results.filter(result => 
                    result.locations && result.locations.length > 0)
                    .map(result => { return result.locations }));
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.