Javascript объект цикла поиска данных - PullRequest
0 голосов
/ 25 сентября 2019

это мой файл.

var tabledata = {
0: {Name: "Server1", PRPName: "NO_PRP", OperatingSystem: "Linux", SLA: "SILVER", Environment: "Accept"}
1: {Name: "Server2", PRPName: "services", OperatingSystem: "Linux", SLA: "SILVER", Environment:"Accept"}
2: {Name: "Server3", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
3: {Name: "Server4", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Accept"}
4: {Name: "Server5", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
5: {Name: "Server6", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
6: {Name: "Server7", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Production"}
7: {Name: "Server8", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}
8: {Name: "Server9", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}
9: {Name: "Server10", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}
10: {Name: "Server11", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
};

Я хочу найти PRPName "sql" и создать новый массив только с этими записями.

код, который я получил такдалеко это:

selectedValue = "sql"
    for (var i = 0; i < tabledata.length; i++) {
                results = [];
                var findItem = Object.values(tabledata).find(function(data){return data.PRPName.toLowerCase() === selectedValue.toLowerCase()});

              }
              console.log(findItem)

Ответы [ 3 ]

2 голосов
/ 25 сентября 2019

способ сделать это сначала создать массив из вашего объекта, а затем вызвать filter для него

var tabledata = {
0: {Name: "Server1", PRPName: "NO_PRP", OperatingSystem: "Linux", SLA: "SILVER", Environment: "Accept"},
1: {Name: "Server2", PRPName: "services", OperatingSystem: "Linux", SLA: "SILVER", Environment:"Accept"},
2: {Name: "Server3", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
3: {Name: "Server4", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Accept"},
4: {Name: "Server5", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
5: {Name: "Server6", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
6: {Name: "Server7", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Production"},
7: {Name: "Server8", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"},
8: {Name: "Server9", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"},
9: {Name: "Server10", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"},
10: {Name: "Server11", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
};

let array = Object.values(tabledata)
let resultArray = array.filter(el => el.PRPName === "sql")
console.log(resultArray)

Я использовал Object.values, чтобы получить массив из вашего объекта, потому что именно он выполняет намеченное преобразование в этом случае

1 голос
/ 25 сентября 2019
var data;
for(i=0;i<11;i++){
    if(tabledata[i].PRPName=='sql')
    data=tabledata[i];
}
console.log(data);
0 голосов
/ 25 сентября 2019
  var tabledata = [{"Name":"Server1","PRPName":"NO_PRP","OperatingSystem":"Linux","SLA":"SILVER","Environment":"Accept"},{"Name":"Server2","PRPName":"services","OperatingSystem":"Linux","SLA":"SILVER","Environment":"Accept"},{"Name":"Server3","PRPName":"NO_PRP","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Accept"},{"Name":"Server4","PRPName":"services","OperatingSystem":"Linux","SLA":"GOLD","Environment":"Accept"},{"Name":"Server5","PRPName":"NO_PRP","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Accept"},{"Name":"Server6","PRPName":"sql","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Accept"},{"Name":"Server7","PRPName":"services","OperatingSystem":"Linux","SLA":"GOLD","Environment":"Production"},{"Name":"Server8","PRPName":"NO_PRP","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Production"},{"Name":"Server9","PRPName":"sql","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Production"},{"Name":"Server10","PRPName":"NO_PRP","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Production"},{"Name":"Server11","PRPName":"sql","OperatingSystem":"Windows","SLA":"GOLD","Environment":"Accept"}];

  function searchData(json, fields, searchValue){
     return json.filter(d => d[fields] == searchValue);
  }

  // Use to Search function
  var searchResult = searchData(tabledata,"PRPName", "sql");
  console.log(searchResult);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...