2017-07-18 16 views
-1

は、私はフィルタリングされたデータの出力を与えるいくつかの機能を書きたい私のデータセットがフィルター多次元配列

var x = [ 
     {"phaseName":"Initiation","phaseID":"595e382f1a1e9124d4e2600c"}, 
     {"phaseName":"Execution","phaseID":"595e38321a1e9124d4e2600d"} 
     ] 

として設定されています。例

var y ="Initiation" 
samplefunction(y) 

については と私はあなたが欲しかった値をすべてのプロパティをテストし、配列をフィルタリングすることができ{"phaseName":"Initiation","phaseID":"595e382f1a1e9124d4e2600c"}

+0

は、あなたがオブジェクトのすべてのプロパティをテストしたいと思いますか? –

+0

'' 'phaseName'''キーフィールドはありますか? – Champ

答えて

0

また、簡単なforループを使用することができます

var x = [{ 
 
    "phaseName": "Initiation", 
 
    "phaseID": "595e382f1a1e9124d4e2600c" 
 
    }, 
 
    { 
 
    "phaseName": "Execution", 
 
    "phaseID": "595e38321a1e9124d4e2600d" 
 
    } 
 
]; 
 

 
var y = "Initiation"; 
 

 
function samplefunction(value) { 
 
    var newArr = new Array(); //creating a new array to store the values 
 
    for (var i = 0; i < Object.keys(x).length; i++) { //looping through the original array 
 
    if (x[i].phaseName == value) {//check if the phase name coresponds with the argument 
 
     newArr.push(x[i]); //push the coressponding value to the new array 
 
    } 
 
    } 
 
    return newArr; //return the new array with filtered values 
 
} 
 
var result = samplefunction(y); 
 
console.log(result);

0

使用Array#filter

var x = [{ 
 
    "phaseName": "Initiation", 
 
    "phaseID": "595e382f1a1e9124d4e2600c" 
 
    }, 
 
    { 
 
    "phaseName": "Execution", 
 
    "phaseID": "595e38321a1e9124d4e2600d" 
 
    } 
 
]; 
 

 
function filter(phaseName) { 
 
    return x.filter(item => { 
 
    return item.phaseName === phaseName; 
 
    }); 
 
} 
 

 

 
console.log(filter('Initiation'));

0

の行全体を取得します。

function filter(array, value) { 
 
    return array.filter(function (object) { 
 
     return Object.keys(object).some(function (key) { 
 
      return object[key] === value; 
 
     }); 
 
    }); 
 
} 
 

 
var data = [{ phaseName: "Initiation", phaseID: "595e382f1a1e9124d4e2600c" }, { phaseName: "Execution", phaseID: "595e38321a1e9124d4e2600d" }]; 
 

 
console.log(filter(data, "Initiation"));