如何在json数据中检查任何列值使用angular js是否存在数组/集合

how to check in json data for any column value is there array/collection present using angular js?

本文关键字:js angular 是否 存在 集合 数组 json 数据 任何列 检查      更新时间:2023-09-26

index.js

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.data =[{"Id":1,"Title":"en-US","Description":"UnitedStates","MyValues":[{"demo":"dish","Id":100,"Value":"Save"}]},
{"Id":1,"Title":"en-UK","Description":"UK","MyValues":[{"demo":"Myvalu","Id":102,"Value":"Delete"}]}]
  $scope.cols = Object.keys($scope.data[0]);
  $scope.notSorted = function(obj){
    if (!obj) {
        return [];
    }
    return Object.keys(obj);
}  
});

我想在$scope.data中检查是否有列值作为数组/集合在上面的代码中,MyValuesarray/collection,我尝试使用angular.isArray(value),但它不起作用,有什么帮助吗?

您可以使用此检查Array.isArray([1,2,3](

$scope.data.map(function(obj){
  return Object.keys(obj).filter(function(k){
       return Array.isArray(obj[k]);
    });
});

上面的代码将为您提供$scope.data数组中每个元素的所有列表,您想要特定的格式吗??

更好的技术是将some()angular.isArray():一起使用

if($scope.data.some(angular.isArray)) {
   //do awesome stuff
}