2

私はこのような配列構造を持っています。オートコンプリートに必要なAngularJS multipleスコープ

[ 
    { 
    "id": "1", 
    "name": "John", 
    "city": "NY" 
    }, 
    { 
    "id": "2", 
    "name": "Gerold", 
    "city": "LA" 
    }, 
    { 
    "id": "3", 
    "name": "Stuart", 
    "city": "Boston" 
    } 
] 

私はオートコンプリートの検索については、以下のような$スコープを必要としています。

$scope.name=["john","Gerold","Stuart"]; 
$scope.city=["NY","LA","Boston"]; 

誰でも、anglejsコントローラを使用してこれを得ることができます。 ありがとうございます。

を名前と都市の配列にユニークなものを使用するようにフィルタを行うことができます

$scope.YourBigArray = [{ 
    "id": "1", 
    "name": "John", 
    "city": "NY" 
}, { 
    "id": "2", 
    "name": "Gerold", 
    "city": "LA" 
}, { 
    "id": "3", 
    "name": "Stuart", 
    "city": "Boston" 
}]; 

$scope.names = $scope.YourBigArray.map(function(object) { 
    return object.name; 
}); 
$scope.cities = $scope.YourBigArray.map(function(object) { 
    return object.city; 
}); 

答えて

0

あなたはMAPを使用することができます..

function filterForDuplicate(things) { 
    return things.filter(function(item, pos) { 
    return things.indexOf(item) === pos; 
    }); 
} 
+0

おかげで...!私はこれを試してみる.. .. !! –

1

使用MAP

$scope.users = [ 
    { 
    "id": "1", 
    "name": "John", 
    "city": "NY" 
    }, 
{ 
    "id": "2", 
    "name": "Gerold", 
    "city": "LA" 
}, 
{ 
    "id": "3", 
    "name": "Stuart", 
    "city": "Boston" 
} 
]; 

$scope.cities = $scope.users.map(function(obj){ 

return obj.city; 
}); 

console.log($scope.cities); 
+0

ありがとう@sisyphus .. !!それは働いて..! –

1

あなたのためにそれを行うヘルパー関数を作成することもできますし、必要な関数ごとにマップを定義する必要はありません。ただ1回の実行でトン(それゆえ少しだけ速い)ここ

サンプル;)あなたの答え@Rakeschandため

var myArray = [ 
    { 
     "id": "1", 
     "name": "John", 
     "city": "NY" 
    }, 
    { 
     "id": "2", 
     "name": "Gerold", 
     "city": "LA" 
    }, 
    { 
     "id": "3", 
     "name": "Stuart", 
     "city": "Boston" 
    } 
] 
function toScope(scopedPropertieNames, array) { 
    scopedPropertieNames.forEach(function(propertyName) { 
     if (! $scope[propertyName]) { 
      $scope[propertyName] = [] 
     } 
    }); 
    array.forEach(function (objecInArray) { 
     scopedPropertieNames.forEach(function(propertyName) { 
      $scope[propertyName].push(objecInArray[propertyName]) 
     }) 
    }); 
} 

toScope(['name', 'city'], myArray); 
console.log($scope) //{name: Array[3], city: Array[3]} 
関連する問題