簡體   English   中英

按標記過濾對象(對象數組內部的數組)

[英]Filter object by tag (array inside an array of objects)

目標

編輯-使問題更清晰

要創建一個搜索,該搜索可以根據項目的標簽縮小搜索結果,並且僅返回具有指定標簽的項目。 示例:['green']將給我所有綠色的項目,包括大,中和小(因為未指定大小),['green','big']僅給我綠色和大的項目。

我遇到了一個場景。 為了簡化起見,我有以下數據

items: [
    { title: 'prod1', tags: [{ value: 'green' }, { value: 'big' }] },
    { title: 'prod2', tags: [{ value: 'blue' }, { value: 'small' }] },
    { title: 'prod3', tags: [{ value: 'yellow' }, { value: 'medium' }, { value: 'big' }] },
    { title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] },
]

我的搜索字段是一個多選輸入,帶有預定義標簽(例如本網站中的標簽字段),該字段的值作為數組輸出。 如果我選擇green的輸出為[“綠色”]如果我選擇greenbig它輸出[“綠色”,“大”]

如果我選擇green標簽,我應該得到這些結果

{ title: 'prod1', tags: [{ value: 'green' }, { value: 'big' }] },
{ title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] },

如果我選擇greenmedium標簽,則會得到這些結果,因為prod4是唯一一個具有綠色和中等標簽的標簽

{ title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] },

如果我選擇greenbig我應該得到

{ title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] },
{ title: 'prod1', tags: [{ value: 'green' }, { value: 'big' }] }

如果未選擇任何內容,則輸出所有項目


原始問題

I ran into a scenario. To simplify things, i have the following data

items: [
  {title: 'one', desc: 'this is one', tags: [{value: 'mytag'},{value: 'one'}]},
  {title: 'two', desc: 'this is two', tags: [{value: 'mytag'},{value: two}]},
  {title: 'three', desc: 'this is three', tags: [{value: 'mytag'},{value: 'three'},{value: 'common'}]},
  {title: 'four', desc: 'this is four', tags: [{value: 'mytag'},{value: 'four'},{value: 'common'}]},
]

我有一個接受標簽的搜索輸入元素,並且輸入的值是一個數組。 輸入的值為['tag','two']我似乎無法弄清楚如何通過標簽過濾項目。

如果我通過標簽二( ['two'] )搜索,我應該返回

filteredItems: [
   {title: 'two', desc: 'this is two', tags: [{value: 'mytag'},{value: two}]},
]

如果我用兩個普通( ['two','common'] )搜索,我應該返回

filteredItems: [
  {title: 'three', desc: 'this is three', tags: [{value: 'mytag'},{value: 'three'},{value: 'common'}]},
  {title: 'four', desc: 'this is four', tags: [{value: 'mytag'},{value: 'four'},{value: 'common'}]},
]

我試着看一些例子,發現一些只過濾頂級項目而不嵌套數組。

謝謝:)順便說一下使用javascript,可以使用lodash。

我的游樂場JS文件:

const _ = require('lodash')

let items = [
    {
        name: 'hello',
        tags: [
            { value: 1 },
            { value: 2 },
        ]
    },
    {
        name: 'bye',
        tags: [
            { value: 2 },
            { value: 4 },
        ]
    }
]
items.map(item =>{

    let test = _.includes(item.tags, 2)
    console.log(test)
// returns false, false
})

(注意:此答案的第一部分與原始問題相關,在編輯之前)

您需要更深一層。 最終的游樂場代碼將一個對象與2進行比較,但應將一個對象屬性與2進行比較。

let test = items.filter(item =>{
    return item.tags.some(obj => obj.value === 2)
})

或者,如果要查找的值在另一個屬性中:

let test = items.filter(item =>{
    return item.tags.some(obj => obj.text === 'two')
})

編輯完您的問題后

您的示例數據可以這樣搜索:

 function search(items, constraints) { return items.filter(item => constraints.every(constraint => item.tags.some(obj => obj.value === constraint) ) ); } const items = [ { title: 'prod1', tags: [{ value: 'green' }, { value: 'big' }] }, { title: 'prod2', tags: [{ value: 'blue' }, { value: 'small' }] }, { title: 'prod3', tags: [{ value: 'yellow' }, { value: 'medium' }, { value: 'big' }] }, { title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] }, ]; console.log(search(items, ["green", "medium"])); 

您需要的邏輯是AND和OR邏輯的組合:對於要匹配給定約束的數組元素,對於給定約束中的每個值(AND),它都應具有一些匹配對象(OR)。

感謝martinoss的這篇文章

解:

使用lodash過濾器功能

let filteredItems = _.filter(items, {tags: [{value: 'green'},{value: 'big'}]}
console.log(filteredItems)

// { title: 'prod1', tags: [{ value: 'green' }, { value: 'big' }] },
// { title: 'prod4', tags: [{ value: 'green' }, { value: 'big' }, { value: 'medium' }] },

這將僅返回具有兩個標簽(綠色和大)的對象。

我確實必須修改數組並將其轉換為對象(我的選擇將值輸出為['green','big']

我做到了以下幾點:

let search = ['green', 'big']

search = search.map(searchTag => {
    return {value: searchTag}
})

// [{value: 'green'},{value: 'big'}]

如果有做香草es6的方式,我很想看看..現在這對我有用

如果搜索中的constraints變量為null,undefined,'',[]或未提供,則下面的代碼應該完成您想要的操作,然后不進行過濾就返回項目。 如果constraints變量是一個字符串,則假設是一個約束,該約束將應用於返回的項目;如果constraints是一個Array,則將應用所有約束。 我試圖以一種自我解釋的方式來命名變量。

var search = (items, constraints) => {
    //no constraints supplied, null, undefined, '', or empty array
  if ( !constraints || constraints === '' || (Array.isArray(constraints) && constraints.length === 0) ) {
    return items;
  }

  let match = false;
  let cons = [];

  //constraints is a string with one constraint add it to an array
  if (typeof constraints === 'string') {
    cons.push(constraints);
  }

  //if constraints is an array of constraints 1 or more
  if (Array.isArray(constraints)) {
    cons.concat(constraints);
    return items.filter( (item) => {
      match = true;
      let itemTags = item.tags.map( (tag) => tag.value );
      cons.forEach( (constraint) => {
        if (constraint === '') return;
        match = (itemTags.indexOf(constraint) > -1 && match);
      } );
      return match;
    } );

  }

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM