छानने के बाद एक सरणी में मर्ज करें


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()विधि पहले एक मानचित्रण समारोह का उपयोग कर प्रत्येक तत्व के नक्शे, फिर एक नई सरणी में परिणाम सपाट। यह एक के समान है 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
यह उल्लेख करना अच्छा है। पॉलफिल के बिना एज और IE में .flatMap काम नहीं करता है।
ज़ेडनार

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 }));
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.