मेरे पास वस्तुओं की एक सरणी है:
और मैं उस वस्तु को हटाना चाहता हूं, जिसकी एक विशिष्ट तिथि है (2020-09-24 इस उदाहरण में) इस तरह के नए ऐरे को आउटपुट करने के लिए एक्सक्लूसिव_डेट्स ऐरे में:
outcome = [
{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [
{ excluded_date: "2020-09-12" },
{
excluded_date: "2020-09-17",
},
],
}
];
इसके लिए मैं डबल फिल्टरिंग का इस्तेमाल करने की सोच रहा था। मैंने कुछ () की भी कोशिश की, लेकिन वह Arrays के लिए है, वस्तुओं की एक सरणी नहीं।
const data = [
{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [
{ excluded_date: "2020-09-12" },
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [
{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
const outcome = data.filter(function (event) {
if (event.excluded_dates) {
return event.excluded_dates.filter(
(date) => date.excluded_date === "2020-09-24"
);
}
});
console.log(outcome);
यह संभोग अपेक्षा के अनुरूप काम नहीं करता है। मैं यहाँ क्या खो रहा हूँ?
7 जवाब
अगर ऑब्जेक्ट में excluded_dates
प्रॉपर्टी नहीं है, तो true
लौटाना सुनिश्चित करें। साथ ही, चूंकि आप केवल एक तिथि की तलाश में हैं, आप Array.prototype.filter
Array.prototype.some()
const outcome = data.filter((event) => {
return !(
event.excluded_dates && // if there is no `excluded_dates` property, return `true`
event.excluded_dates.some( // otherwise, try to find the date. if found, return `false`
({ excluded_date }) => excluded_date === "2020-09-24"
)
);
});
कोड स्निपेट उदाहरण:
const data = [{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [{
excluded_date: "2020-09-12"
},
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
const outcome = data.filter((event) => {
return !(
event.excluded_dates &&
event.excluded_dates.some(
({ excluded_date }) => excluded_date === "2020-09-24"
)
);
});
console.log(outcome);
आप सही रास्ते पर थे, केवल स्थिति बदलने की जरूरत है। फ़िल्टर हमेशा आइटम लौटाता है जहां स्थिति मेल खाती है। आपको यह जांचने की आवश्यकता है कि क्या बहिष्कृत_डेट गलत है या बहिष्कृत_डेट सरणी में दिनांक नहीं है। तो आंतरिक फ़िल्टर में यदि हमें लंबाई 0 मिलती है, तो इसका मतलब है कि दिनांक सरणी में मौजूद नहीं है और इसलिए अपेक्षित आउटपुट में शामिल करें।
const data = [{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [{
excluded_date: "2020-09-12"
},
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
const outcome = data.filter(event => (!event.excluded_dates || event.excluded_dates.filter(
(date) => date.excluded_date === "2020-09-24"
).length == 0))
console.log(outcome);
आप सही रास्ते पर थे। तुम भी कुछ () का उपयोग बहिष्कृत_डेट्स सरणी के लिए कर सकते हैं। यहाँ एक कार्यशील कार्य है
data.filter((d) => {
if (Array.isArray(d.excluded_dates)) {
return d.excluded_dates.some((date) => date.excluded_date === '2020-09-24');
}
return true;
});
हम कुछ इस तरह भी कर सकते हैं, फ़िल्टर के अंदर लूप के लिए केवल तभी सही लौटने के लिए उपयोग किया जाता है जब कोई भी तारीख बहिष्कृत तिथि से मेल नहीं खाती
const data = [
{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [
{ excluded_date: "2020-09-12" },
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [
{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
const outcome = data.filter(function (event) {
if (event.excluded_dates) {
for(let i=0; i<event.excluded_dates.length; i++){
if(event.excluded_dates[i].excluded_date == "2020-09-24")
return false;
}
}
return true;
});
console.log(outcome);
यदि ईवेंट में excluded_dates
प्रॉपर्टी नहीं है, तो true
लौटाएं, क्योंकि जिन्हें आप हर परिदृश्य में रखना चाहेंगे।
नेस्टेड फ़िल्टर के बजाय जाँच करें कि क्या excluded_dates
सरणी में प्रत्येक excluded_date
, "2020-09-24"
के बराबर नहीं है Array.prototype.every
विधि, जो हर कॉलबैक का मूल्यांकन किए जाने पर सही या गलत लौटाती है सही या गलत।
const data = [{
title: '"test1"',
excluded_dates: false,
},
{
title: '"test2"',
excluded_dates: [{
excluded_date: "2020-09-12"
},
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
const outcome = data.filter(function(event) {
if (event.excluded_dates) {
return event.excluded_dates.every(
(date) => date.excluded_date !== "2020-09-24"
);
}
return true;
});
console.log(outcome);
मैं filter
का उपयोग करूंगा:
let data = [
{ index: 0, exclude: ['Monday'] },
{ index: 1, exclude: ['Tuesday', 'Thursday'] },
{ index: 2, exclude: ['Wednesday'] },
];
data = data.filter(el => !el.exclude.includes('Tuesday'));
console.log(data);
// data = { index: 0, exclude: ['Monday'] }, { data: 2, exclude: ['Wednesday'] }
Foreach और फ़िल्टर ऑपरेटरों का उपयोग करने का प्रयास किया
const data = [
{
title: '"test2"',
excluded_dates: [
{ excluded_date: "2020-09-12" },
{
excluded_date: "2020-09-17",
},
],
},
{
title: '"test3"',
excluded_dates: [
{
excluded_date: "2020-09-16",
},
{
excluded_date: "2020-09-24",
},
],
},
];
data.forEach(function(item, idx) {
item.excluded_dates.filter(function(item1) {
if( item1.excluded_date === "2020-09-24" ) {
return data.pop(idx);
}
});
});
console.log(data);