英文:
How to remove element that are coming inside other array elements
问题
Here's the translated code part without the explanation:
// 国家数组(输入)
['US','Canada','Mexico','North Korea','Korea','Germany'];
// 期望的输出
['US','Canada','Mexico','North Korea','Germany'];
// 国家数组(输入)
['North America','France','India','America'];
// 期望的输出
['North America','France','India'];
你可以尝试使用以下方法来获得期望的输出:
// 示例代码
const countries = ['US','Canada','Mexico','North Korea','Korea','Germany'];
const filteredCountries = countries.filter(country => !['Korea', 'America'].includes(country));
// 现在filteredCountries包含了期望的输出
英文:
I have an array of countries coming from the database.
Here are 2 examples of the input, and output I'm expecting:
// countries array (input)
['US','Canada','Mexico','North Korea','Korea','Germany'];
// expected output
['US','Canada','Mexico','North Korea','Germany'];
// countries array (input)
['North America','France','India','America'];
// expected output
['North America','France','India'];
I want to remove Korea
and America
from the respective arrays as I already have North Korea
and North America
corresponding to those elements.
I have tried includes()
method but it's not working as it checks for the exact value.
How can I get the expected output?
答案1
得分: 1
以下是代码的翻译部分:
要filter()
数组中的国家,并在其他国家includes()
国家名称时删除国家。
function solution(countries) {
return countries.filter((part) => (
!countries.some(whole => whole !== part && whole.includes(part))
));
}
console.log(solution(['US','Canada','Mexico','North Korea','Korea','Germany']));
console.log(solution(['North America','France','India','America']));
whole !== part
是为了防止国家与自己匹配时使用whole.includes(part)
。
英文:
So you want to filter()
the array of countries and remove a country if some()
other country includes()
the country name.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function solution(countries) {
return countries.filter((part) => (
!countries.some(whole => whole !== part && whole.includes(part))
));
}
console.log(solution(['US','Canada','Mexico','North Korea','Korea','Germany']));
console.log(solution(['North America','France','India','America']));
<!-- end snippet -->
whole !== part
is there is to prevent a country from matching against itself with whole.includes(part)
.
答案2
得分: 0
以下是翻译好的部分:
如果后一个元素在前一个元素中,则应将其移除。是吗?您可以使用 slice()
获取前面元素的数组,并使用 filter()
检查每个元素是否包含特定字符串,就像这样。
const first = ['美国', '加拿大', '墨西哥', '朝鲜', '韩国', '德国'];
const second = ['北美洲', '法国', '印度', '美国'];
function removeSimilar(countries) {
const result = [];
countries.forEach((country, i) => {
const previousSlice = countries.slice(0, i);
const filtered = previousSlice.filter(el => el.includes(country));
if (filtered.length == 0)
result.push(country);
});
return result;
}
console.log(removeSimilar(first)); // ['美国', '加拿大', '墨西哥', '朝鲜', '德国']
console.log(removeSimilar(second)); // ['北美洲', '法国', '印度']
英文:
Well, if later element is 'included' in previous element's', then it should be removed. Is it right? You can get an array of previous elements with slice()
and check whether each element includes certain string with filter()
like this.
const first = ['US','Canada','Mexico','North Korea','Korea','Germany'];
const second = ['North America','France','India','America'];
function removeSimilar(conutries) {
const result = [];
conutries.forEach((country, i) => {
const previousSlice = conutries.slice(0, i);
const filtered = previousSlice.filter(el => el.includes(country));
if(filtered.length == 0)
result.push(country);
});
return result;
}
console.log(removeSimilar(first)); // ['US', 'Canada', 'Mexico', 'North Korea', 'Germany']
console.log(removeSimilar(second)); // ['North America', 'France', 'India']
答案3
得分: 0
我们可以使用reduce函数来执行此操作,其中我们检查其他元素是否包含该字符串。这假设更具体的字符串后面出现在数组中,即"America"会出现在"North America"之后。
const array1 = ['US', 'Canada', 'Mexico', 'North Korea', 'Korea', 'Germany'];
const array2 = ['North America', 'France', 'India', 'America'];
const removeFunc = (countries) => {
return countries.reduce((acc, country) => {
if (acc.some(elem => elem.includes(country))) return acc;
return [...acc, country];
}, [])
}
console.log(removeFunc(array1));
console.log(removeFunc(array2));
英文:
We can just use a reduce to perform that action where we check if other element includes the string. This assuming that the more especific comes later in the array, ie America will appear after north america.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array1 = ['US','Canada','Mexico','North Korea','Korea','Germany'];
const array2 = ['North America','France','India','America'];
const removeFunc = (countries) => {
return countries.reduce((acc, country) => {
// shorter but maybe messier return option
// return (acc.some(elem => elem.includes(country))) ? acc : [...acc, country];
if(acc.some(elem => elem.includes(country))) return acc;
return [...acc, country];
}, [])
}
console.log(removeFunc(array1));
console.log(removeFunc(array2));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论