簡體   English   中英

針對Javascript中的另一個對象過濾對象數組

[英]Filter array of objects against another object in Javascript

我有一系列對象

var data = [
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "122",
      "calories_max": "250"
    },
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "150",
      "calories_max": "280"
    },
    {
      "body_focus": "lower",
      "difficulty": "two",
      "calories_min": "100",
      "calories_max": "180"
    },
    {
      "body_focus": "total",
      "difficulty": "four",
      "calories_min": "250",
      "calories_max": "350"
    }
]

我想針對另一個對象過濾該對象數組

var myObj = {
    "upper": true,
    "three": true
}

所以現在myObj有一個鍵"upper""three" ,它們的值是true 因此,基於這些值,我想創建一個函數來獲取data數組中的所有對象,其鍵"body_focus"值為"upper""difficulty"鍵的值為"three"

因此該函數應僅返回這些對象

[
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "122",
      "calories_max": "250"
    },
    {
      "body_focus": "upper",
      "difficulty": "three",
      "calories_min": "150",
      "calories_max": "280"
    }
]

這就是我試圖解決問題的方式

var entry;
var found = [];
for (var i = 0; i < data.length; i++) {
    entry = data[i];
    for(var key in myObj) {
        if(entry.body_focus.indexOf(key) !== -1) {
            found.push(entry);
            break;  
        }
    }
}

我上面的代碼僅檢查body_focus ,那么如何同時檢查body_focusdifficulty 可能看起來很傻,但是我被困了幾個小時,找不到解決方案

您可以使用一個對象進行搜索,該對象給出鍵和所需的值,例如

search = {
    body_focus: "upper",
    difficulty: "three"
}

然后遍歷數組,並使用實際對象的值檢查search所有屬性。 如果所有搜索條件都匹配,則返回true

 var data = [{ body_focus: "upper", difficulty: "three", calories_min: "122", calories_max: "250" }, { body_focus: "upper", difficulty: "three", calories_min: "150", calories_max: "280" }, { body_focus: "lower", difficulty: "two", calories_min: "100", calories_max: "180" }, { body_focus: "total", difficulty: "four", calories_min: "250", calories_max: "350" }], search = { body_focus: "upper", difficulty: "three" }, result = data.filter(function (o) { return Object.keys(search).every(function (k) { return o[k] === search[k]; }); }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

我認為這應該可以解決問題。

var myObj = {
    "upper": true,
    "three": true
}
var found = [];
for (var i = 0; i < data.length; i++) {
    entry = data[i];
    if(myObj[entry.body_focus] && myObj[entry.difficulty]) {
      found.push(entry);
    }
}

使用myObj [entry.body_focus]來檢查myObj是否具有upper屬性,以及它是否為true。 同樣有困難。

此解決方案使用Array.prototype.filterArray.prototype.every的組合,以將數據對象中的所有值與param對象中的鍵匹配。

 var data = [ {"body_focus": "upper", "difficulty": "three", "calories_min": "122", "calories_max": "250"}, {"body_focus": "upper","difficulty": "three","calories_min": "150","calories_max": "280"}, {"body_focus": "lower","difficulty": "two","calories_min": "100","calories_max": "180"}, {"body_focus": "total","difficulty": "four","calories_min": "250","calories_max": "350"} ] var params = { "upper": true, "three": true } function filter(params, data) { const keys = Object.keys(params) return data.filter(item => keys.every(value => ~Object.values(item).indexOf(value)) ) } console.log( filter(params, data) ) 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> 

妮娜的答案應該對您有用。 但是,如果您有興趣,可以使用另一種方法:

 var data = [{ "body_focus": "upper", "difficulty": "three", "calories_min": "122", "calories_max": "250" }, { "body_focus": "upper", "difficulty": "three", "calories_min": "150", "calories_max": "280" }, { "body_focus": "lower", "difficulty": "two", "calories_min": "100", "calories_max": "180" }, { "body_focus": "total", "difficulty": "four", "calories_min": "250", "calories_max": "350" }]; var search = { body_focus: "upper", difficulty: "three" }; var results=$.grep(data,function(datum,index){ var sat=true; Object.keys(search).forEach(function(el){ if(datum[el]!=search[el]) { sat=false; return false; } }); return sat; }); console.log(results); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

關鍵是您可以使用$ .grep編寫自定義過濾器函數,並使用第一個參數'datum'或第二個'index'檢查某些條件,然后返回true表示過濾器匹配,或者返回false表示其他情況

最好將myObj對象的結構更改為更多

let myObj = 
{
    body_focus: "upper",
    difficulty: "three"
}     

作為您的搜索對象。 (正如答案之一所述)

然后,您可以對原始對象數組使用filter ,以獲取所需的內容。

 let results = data.filter(item => item.body_focus === myObj.body_focus && item.difficulty === myObj.difficulty); 

暫無
暫無

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

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