JSON仅选择某些元素

时间:2018-04-11 15:15:30

标签: jquery json

我只是选择了JSON列表中的某些元素。对于下面的示例,我只想要其中max == 0的元素名称。我尝试了以下select语句,但它返回所有元素max == 0或max == 1.任何想法?

选择声明:

jq 'select(.[].max == 0)'

示例JSON:

[
  {
    "name": "PATCHING-vhdb",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhi",
    "ctm": "controlm-server",
    "available": "0",
    "max": 0,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhr",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhs",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  }
]

4 个答案:

答案 0 :(得分:0)

不确定您是否希望以jq或其他任何方式使用此功能,但这是通过max === 0过滤数据的简单方法:

let data = [{
    "name": "PATCHING-vhdb",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhi",
    "ctm": "controlm-server",
    "available": "0",
    "max": 0,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhr",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhs",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  }
];

const result = data.reduce((result, item) => {
  if (item.max === 0) {
    result.push(item.name);
  }

  return result;
}, []);

console.log(result);

答案 1 :(得分:0)

当您标记jquery时......您可以使用filter()



var data = [{
    "name": "PATCHING-vhdb",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhi",
    "ctm": "controlm-server",
    "available": "0",
    "max": 0,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhr",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhs",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  }
];

$(document).ready(function() {

  var result = $(data).filter(function(i, n) {
    return n.max === 1
  });

  console.log(result);

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以在Array上使用javascripts filter方法,如下所示:

SELECT c.CustomerID, c.FirstName + ' ' + c.LastName as CustomerName,
       l. StreetAddress1, l. StreetAddress2, l.City, l.State, l.Zip, l.Country
FROM Customers c
LEFT JOIN CustomerLocations cl ON c.CustomerID = cl.CustomerID
LEFT JOIN Locations l ON cl.LocationID = l.LocationID

答案 3 :(得分:0)

使用JavaScript:

&#13;
&#13;
var jsonObj = [{
		"name": "PATCHING-vhdb",
		"ctm": "controlm-server",
		"available": "1",
		"max": 1,
		"workloadPolicy": "N/A"
	},
	{
		"name": "PATCHING-vhi",
		"ctm": "controlm-server",
		"available": "0",
		"max": 0,
		"workloadPolicy": "N/A"
	},
	{
		"name": "PATCHING-vhr",
		"ctm": "controlm-server",
		"available": "1",
		"max": 1,
		"workloadPolicy": "N/A"
	},
	{
		"name": "PATCHING-vhs",
		"ctm": "controlm-server",
		"available": "1",
		"max": 1,
		"workloadPolicy": "N/A"
	}
];

var res = jsonObj.filter(obj => obj.max === 0);

for (var i in res) {
  console.log("Element Name :", res[i].name);
}
&#13;
&#13;
&#13;

使用JQuery:

&#13;
&#13;
var jsonObj = [{
    "name": "PATCHING-vhdb",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhi",
    "ctm": "controlm-server",
    "available": "0",
    "max": 0,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhr",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  },
  {
    "name": "PATCHING-vhs",
    "ctm": "controlm-server",
    "available": "1",
    "max": 1,
    "workloadPolicy": "N/A"
  }
];

$(document).ready(function() {

  var res = $(jsonObj).filter(function(index, item) {
    return item.max === 0
  });

  console.log("Element name :", res[0].name);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;