var allEmojis = [dog, toucan, flamingo, lion, tiger, duck, elephant, zebra]
몽구스에서 얻은 문자열 배열과 객체 배열이 있습니다. 개체 배열의 각 개체에는 allEmojis 배열에서 필터링 된 이모 지 속성이 있습니다. (아래는 객체의 배열입니다)
다음과 같이 배열을 필터링하고 싶습니다.
객체 배열이 단순히 'ArrayOfObjects'라고 가정 해 보겠습니다.
var JohnsEmojis = allEmojis.filter(function(emoji) {
return !ArrayOfObjects.includes(emoji) /*=>>> where the object in ArrayOfObjects
containing the emoji has an id of John not yoyoyo (yes i know, dumb name); */
})
var yoyoyoEmojis = allEmojis.filter(function(emoji) {
return !ArrayOfObjects.includes(emoji) /*=>>> where the object in ArrayOfObjects
containing the emoji has an id of yoyoyo not John; */
})
내 코드는 두 가지로 실패합니다. include
문자열 배열이있는 객체 배열에는를 사용할 수 없습니다 . 또한 이것이 작동했다고 가정하면 지정된 ID를 가진 개체를 기준으로 필터링하지 않습니다. 이 작업을 수행하는 가장 효율적인 방법을 찾을 수 있기를 바랐습니다.
allEmojis
따옴표없이 썼다
var allEmojis = [dog, toucan, flamingo, lion, tiger, duck, elephant, zebra]
문자열 배열은
var allEmojis = ["dog", "toucan", "flamingo", "lion", "tiger", "duck", "elephant", "zebra"]
이것이 문제가되지 않고 allEmojis
실제로 arrayOfObjects
key 아래의 모든 객체에 있는 문자열을 포함하는 object.emoji
경우 다음 allEmojis
과 arrayOfObjects
같이 교차를 필터링 할 수 있습니다.
var filtered = allEmojis.filter(function(e) {
return !!arrayOfObjects.find(function(o) {
return o.emoji === e;
});
};
다음과 같이 쓸 수도 있습니다.
var filtered = arrayOfObjects
.filter(function(o) { return allEmojis.includes(o.emoji) })
.map(function(o) { return o.emoji }); // convert objects to strings
성능이 더 좋을 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다