Reputation:
I have to filter the object based on the values of array.
This is my object
$scope.myObj = {
en_US: "English",
es_ES: "Spanish",
pt_PT: "Portuguese",
fr_FR: "French",
de_DE: "German"
}
This is my array
$scope.myArr=["en_US","es_ES"]
I want to filter my object using my array? How to do that in efficient way? Thanks in advance.
Upvotes: 2
Views: 100
Reputation: 64933
Probably you're looking to match those ISO language codes found in myArr
against myObj
. Hence, Array.prototype.map
should be enough:
var myArr = ["en_US", "es_ES"];
var myObj = {
en_US: "English",
es_ES: "Spanish",
pt_PT: "Portuguese",
fr_FR: "French",
de_DE: "German"
};
var matchedLangs = myArr.map(lang => ({
[lang]: myObj[lang]
}));
console.log(matchedLangs);
This approach will give you an array of objects where properties are the ISO language codes and the property values their English name.
If you're looking for an object result, let's add Array.prototype.reduce
:
var myArr = ["en_US", "es_ES"];
var myObj = {
en_US: "English",
es_ES: "Spanish",
pt_PT: "Portuguese",
fr_FR: "French",
de_DE: "German"
};
// #1 Array.prototype.map returns an array of matched language codes to
// their english names
//
// #2 Array.prototype.reduce receives the array from #1 and transforms it
// creating a new "result" object. Object.assign mixes the "match" object
// (an object whose property is the ISO code and the value the English name)
// with the "result" object, which represents the accumulated result
var matchedLangs = myArr.map(lang => ({
[lang]: myObj[lang]
})).reduce((result, match) => Object.assign(result, match));
console.log(matchedLangs);
// Everything can be also be simplified to just use Array.prototype.reduce:
var matchedLangs2 = myArr.reduce((result, lang) =>
Object.assign(result, {
[lang]: myObj[lang]
}), {});
console.log(matchedLangs2);
{ [blah]: "x" }
thing?Since ECMA-Script 2015, you can declare properties using non-literal values.
See the following snippet:
var propertyName = "hello";
var obj = { [propertyName]: "world" };
console.log(obj);
Upvotes: 0
Reputation: 386540
You could iterate the given keys and build a new object.
var $scope = {}, result;
$scope.myObj = { en_US: "English", es_ES: "Spanish", pt_PT: "Portuguese", fr_FR: "French", de_DE: "German" };
$scope.myArr = ["en_US", "es_ES"];
result = $scope.myArr.reduce(function (o, k) {
o[k] = $scope.myObj[k];
return o;
}, {});
console.log(result);
ES6 with Object.assign
var $scope = {}, result;
$scope.myObj = { en_US: "English", es_ES: "Spanish", pt_PT: "Portuguese", fr_FR: "French", de_DE: "German" };
$scope.myArr = ["en_US", "es_ES"];
result = $scope.myArr.reduce((o, k) => Object.assign(o, { [k]: $scope.myObj[k] }), {});
console.log(result);
Even shorter with ES6, Array#map
and spread syntax ...
.
var $scope = {}, result;
$scope.myObj = { en_US: "English", es_ES: "Spanish", pt_PT: "Portuguese", fr_FR: "French", de_DE: "German" };
$scope.myArr = ["en_US", "es_ES"];
result = Object.assign(...$scope.myArr.map(k => ({ [k]: $scope.myObj[k] })));
console.log(result);
Upvotes: 3
Reputation: 797
You can use Array.prototype.filter() function like
myArr.filter(function (o, k) {
o[k] = myObj[k];
return o;
}, {});
This will give result like [0:"en_US",1:"es_ES"]
refer this https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for more information
Upvotes: 0
Reputation: 3501
As you appear to be using AngularJS you can use the angular.forEach
function:
var myObj = {
en_US: "English",
es_ES: "Spanish",
pt_PT: "Portuguese",
fr_FR: "French",
de_DE: "German"
};
var myArr = ["en_US", "es_ES"];
angular.forEach(myObj, function(value, key) {
// if the key doesn't appear in `myArr` delete the key-pair from the object
if (myArr.indexOf(key) === -1) {
// delete the property from the object
delete myObj[key];
}
});
console.log(myObj);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Upvotes: 0
Reputation: 41893
You can use Array.prototype.reduce()
to get the desired result.
var myObj = {en_US: "English", es_ES: "Spanish", pt_PT: "Portuguese", fr_FR: "French", de_DE: "German"},
myArr = ["en_US","es_ES"];
myObj = Object.keys(myObj).reduce(function(s,a) {
if (myArr.indexOf(a) == -1) {
s[a] = myObj[a];
}
return s;
}, {});
console.log(myObj);
Upvotes: 0