Javascript 如何从布尔对象创建新数组
我很确定也有类似的情况,但我无法在我的范围内找出这一点。我有一个数组,需要基于对象创建另一个数组(在本例中为Javascript 如何从布尔对象创建新数组,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,我很确定也有类似的情况,但我无法在我的范围内找出这一点。我有一个数组,需要基于对象创建另一个数组(在本例中为BannerAtivo:true) 我的JSON: [{ "BannerID": 0, "BannerImage": "http://hotsite/example2.png", "BannerMin": "http://hotsite/example.png", "BannerTitulo": "Xurupita Style", "BannerDe
BannerAtivo:true
)
我的JSON:
[{
"BannerID": 0,
"BannerImage": "http://hotsite/example2.png",
"BannerMin": "http://hotsite/example.png",
"BannerTitulo": "Xurupita Style",
"BannerDesc": "Lorem Ipsum Dolor Siamet",
"BannerAtivo": false,
"SetorId": 1
}, {
"BannerID": 2,
"BannerImage": "http://hotsite/example2.jpg",
"BannerMin": "http://hotsite/example.jpg",
"BannerTitulo": "Chihuahua",
"BannerDesc": "Role",
"BannerAtivo": true,
"SetorId": 1
}]
我正在尝试以下代码:
var data = response.data;
$scope.banners = data;
var a = $scope.banners;
function check(a) {
return a.BannerAtivo = true;
console.log(BannerAtivo);
}
我想要的是:仅在对象
BannerAtivo
中为true
的列表中显示它。我已经尝试过过滤器,但不知道如何制作。我甚至不能在控制台上显示它。我知道这个问题很愚蠢,是的,我是javascript和angular方面的新手。抱歉。您实际上是在check
函数中将true
赋值给变量,而不是实际检查值。另外,console.log
语句在return
语句之后没有任何意义,因为它表示永远无法访问的代码
现在要解决您的问题,只需根据布尔值过滤数组,如下所示:
$scope.banners = response.data.filter((value) => {
return value.BannerAtivo;
});
您实际上是在
check
函数中将true
赋值给变量,而不是实际检查值。另外,console.log
语句在return
语句之后没有任何意义,因为它表示永远无法访问的代码
现在要解决您的问题,只需根据布尔值过滤数组,如下所示:
$scope.banners = response.data.filter((value) => {
return value.BannerAtivo;
});
运行此代码,并在控制台中检入返回的仅具有真值的对象数组。 给你:
<html>
<body>
<script type="text/javascript">
//Assuming you got response here
var data = [
{
"BannerID": 0,
"BannerImage": "http://hotsite/example2.png",
"BannerMin": "http://hotsite/example.png",
"BannerTitulo": "Xurupita Style",
"BannerDesc": "Lorem Ipsum Dolor Siamet",
"BannerAtivo": false,
"SetorId": 1
},
{
"BannerID": 2,
"BannerImage": "http://hotsite/example2.jpg",
"BannerMin": "http://hotsite/example.jpg",
"BannerTitulo": "Chihuahua",
"BannerDesc": "Role",
"BannerAtivo": true,
"SetorId": 1
},
{
"BannerID": 2,
"BannerImage": "http://hotsite/example2.jpg",
"BannerMin": "http://hotsite/example.jpg",
"BannerTitulo": "Chihuahua",
"BannerDesc": "Role",
"BannerAtivo": true,
"SetorId": 1
}
];
var returnTrueData = function () {
var result = [];
for (var i = 0; i < data.length; i++) { //looping through array of objects
for (key in data[i]) { //looping through each object
if(key === "BannerAtivo" && data[i][key] === true) { //If the key is "BannerAtivo" and it has value 'true'
result.push(data[i]); //push the whole object into the result array
}
}
}
console.dir(result);
return result;
}
returnTrueData();
</script>
</body>
//假设你得到了回应
风险值数据=[
{
“BannerID”:0,
“BannerImage”:http://hotsite/example2.png",
“班纳明”:http://hotsite/example.png",
“班纳蒂图罗”:“徐鲁皮塔风格”,
“BannerDesc”:“Lorem Ipsum Dolor Siamet”,
“bannarativo”:错误,
“SetorId”:1
},
{
"旗帜":二,,
“BannerImage”:http://hotsite/example2.jpg",
“班纳明”:http://hotsite/example.jpg",
“班纳蒂图罗”:“吉娃娃”,
“BannerDesc”:“角色”,
“BannerAtivo”:没错,
“SetorId”:1
},
{
"旗帜":二,,
“BannerImage”:http://hotsite/example2.jpg",
“班纳明”:http://hotsite/example.jpg",
“班纳蒂图罗”:“吉娃娃”,
“BannerDesc”:“角色”,
“BannerAtivo”:没错,
“SetorId”:1
}
];
var returnTrueData=函数(){
var结果=[];
对于(var i=0;i
为了更好地理解,请参阅如何循环对象或对象数组运行此代码,并在控制台中签入返回的只有真值的对象数组。 给你:
<html>
<body>
<script type="text/javascript">
//Assuming you got response here
var data = [
{
"BannerID": 0,
"BannerImage": "http://hotsite/example2.png",
"BannerMin": "http://hotsite/example.png",
"BannerTitulo": "Xurupita Style",
"BannerDesc": "Lorem Ipsum Dolor Siamet",
"BannerAtivo": false,
"SetorId": 1
},
{
"BannerID": 2,
"BannerImage": "http://hotsite/example2.jpg",
"BannerMin": "http://hotsite/example.jpg",
"BannerTitulo": "Chihuahua",
"BannerDesc": "Role",
"BannerAtivo": true,
"SetorId": 1
},
{
"BannerID": 2,
"BannerImage": "http://hotsite/example2.jpg",
"BannerMin": "http://hotsite/example.jpg",
"BannerTitulo": "Chihuahua",
"BannerDesc": "Role",
"BannerAtivo": true,
"SetorId": 1
}
];
var returnTrueData = function () {
var result = [];
for (var i = 0; i < data.length; i++) { //looping through array of objects
for (key in data[i]) { //looping through each object
if(key === "BannerAtivo" && data[i][key] === true) { //If the key is "BannerAtivo" and it has value 'true'
result.push(data[i]); //push the whole object into the result array
}
}
}
console.dir(result);
return result;
}
returnTrueData();
</script>
</body>
//假设你得到了回应
风险值数据=[
{
“BannerID”:0,
“BannerImage”:http://hotsite/example2.png",
“班纳明”:http://hotsite/example.png",
“班纳蒂图罗”:“徐鲁皮塔风格”,
“BannerDesc”:“Lorem Ipsum Dolor Siamet”,
“bannarativo”:错误,
“SetorId”:1
},
{
"旗帜":二,,
“BannerImage”:http://hotsite/example2.jpg",
“班纳明”:http://hotsite/example.jpg",
“班纳蒂图罗”:“吉娃娃”,
“BannerDesc”:“角色”,
“BannerAtivo”:没错,
“SetorId”:1
},
{
"旗帜":二,,
“BannerImage”:http://hotsite/example2.jpg",
“班纳明”:http://hotsite/example.jpg",
“班纳蒂图罗”:“吉娃娃”,
“BannerDesc”:“角色”,
“BannerAtivo”:没错,
“SetorId”:1
}
];
var returnTrueData=函数(){
var结果=[];
对于(var i=0;i