Remove all elements from object except specified key? - javascript

I have an object:
"languages": {
"en":["au", "uk"],
"de":["de"],
....
}
How can I remove everything but a specified key, so if I specify 'en' I just want an object that contains "en":["au", "uk"]

General solution for the original question of 'how do I remove all keys except specified keys' (refined from Rajaprabhu's answer):
validKeys = [ 'a', 'b', 'c' ];
userInput = { "a":1, "b":2, "c":3, "d":4, "e":5 }
Object.keys(userInput).forEach((key) => validKeys.includes(key) || delete userInput[key]);

Simply, you could create a new object with specified field;
var key = 'en';
var o = {
"languages": {
"en": ["au", "uk"],
"de": ["de"]
}
}
var res = {}
res[key] = o.languages[key];

Try to delete the unwanted properties,
var obj = { "languages": { "en":["au", "uk"],"de":["de"] }};
Object.keys(obj.languages).forEach(function(itm){
if(itm != "en") delete object.languages[itm];
});

A simple loop using delete will do it.
var key = 'en';
for (var k in obj.languages) {
if (obj.languages.hasOwnProperty(k) && k != key) {
delete obj.languages[k];
}
}

Was searching for the answer and the previous ones helped me, just adding a functional version of the code that I needed:
function returnNewObjectOnlyValidKeys(obj, validKeys) {
const newObject = {};
Object.keys(obj).forEach(key => {
if (validKeys.includes(key)) newObject[key] = obj[key];
});
return newObject;
}

Related

Compare key values within object for duplicate updated

This is a follow up post from Compare key values within object for duplicate for a follow up answer.
I have an object:
myObj = {
attendent-0-id:"123",
attendent-0-name:"Bob Smith",
attendent-1-id:"1234",
attendent-1-name:"Alex Smith",
attendent-2-id:"123",
attendent-2-name:"Charlie Smith",
attendent-0-id:"123",
attendent-0-name:"John Smith",
attendent-maxGuest:1,
attendent-party-name:"",
}
Thanks to help on here (Rick) I was able to get 90% of the way there.
function errorOnDuplicateIds(obj) {
const map = {};
const pattern = /^attendent-\d+-id$/;
for (const key of Object.keys(obj)) {
if (pattern.test(key)) {
const value = obj[key]
if (value in map) {
map[value] = [map[value], key];
} else {
map[value] = key
}
}
}
return map;
}
I am getting a return of:
array:[
0:(2) ["attendent-0-name", "attendent-1-name"]
1:"attendent-2-name"
]
but I am looking for:
array:[
0:(2) ["attendent-0-name", "attendent-1-name", "attendent-2-name"]
]
The issue I am having is that while this works if there are two matching keys it will not work (Correctly) if there are three or more.
If you want to have an array of all matches for each key in you map, you need to start by setting an array when you find a key the first time. On subsequent matches, just push into that array:
const myObj = {'attendent-0-id': "1234",'attendent-0-name': "Bob Smith",'attendent-1-id': "123",'attendent-1-name': "Alex Smith",'attendent-2-id': "123",'attendent-2-name': "Charlie Smith",'attendent-maxGuest': 1,'attendent-party-name': "",};
function errorOnDuplicateIds(obj) {
const map = {};
const pattern = /^attendent-\d+-id$/;
for (const key of Object.keys(obj)) {
if (pattern.test(key)) {
const value = obj[key]
if (value in map) {
map[value].push(key); // push a new value
} else {
map[value] = [key] // set it to an array
}
}
}
/* if you only want lengths > 1
otherwise just return map */
let filtered = Object.entries(map)
.reduce((a, [key, value]) => value.length > 1 ? Object.assign(a, {[key]: value}) : a, {})
return filtered;
}
console.log(errorOnDuplicateIds(myObj));
If you are only interested in values with more than one hit, you can reduce() down to a map with only values of length greater than one, which is what the last bit in the snippet does.

how to create a map with unique keys from a parsed object in javascript es6/2015?

Lets say I receive a parsed json like below:
[{"a":1},{"a":2},{"a":3}]
The keys are the same which is a.
How do I make each a unique so that the map is usable?'
EDIT1:
Results I want:
let myMap = {}; //I declare my variable
//Then I fetch a json and parse it
fetch(link)
.then(function(response) {
return response.json(); //parse the json string
}).then(function(json) {
myMap = json; //set it to myMap to be used
}
For some reason I having duplicate keys although you guys said the json is unique. Do I have to set the json string to myMap first and then only parse it?
Basically you can use an Object as hash table
var data = [{ "a": 1 }, { "a": 2 }, { "a": 3 }],
object = Object.create(null);
data.forEach(function (el) {
object[el.a] = el;
});
console.log(object);
Or a Map
var data = [{ "a": 1 }, { "a": 2 }, { "a": 3 }],
map = new Map;
data.forEach(function (el) {
map.set(el.a, el);
});
console.log(map.get(1));
The advantage of Map over an Object is, the key can be anything. The key is not converted to string. Maps can have an object or other primitive or not primitive values as key.
Also if you have a single value list or want to make sure it IS unique you can use the index supplied like this:
obj.map((item, index) =>
...
)}
Maybe this?
[{a:1},{a:2},{a:3}].map(function(item, index) { item.id = index; return item; });
Map in javascript doesnot need a unique id, it will iterate through all the value. so it will iterate through all the objects irrespective the fact that the key is same
eg:
var kvArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}]
var reformattedArray = kvArray.map(function(obj){
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});
Well, [{a:1},{a:2},{a:3}] is already unique... but, It's an Array.
so you cannot access an object {a:2, ...} directly but find index with looping.
If I understand your question right way... you want to make new MAP with unique key a how about this way? - reduce can help us. :)
btw, Nina Scholz's answer is right.
let myMap = {}; //I declare my variable
//Then I fetch a json and parse it
fetch(link)
.then(function(response) {
return response.json(); //parse the json string
}).then(function(json) {
// myMap = json; //set it to myMap to be used
myMap = json.reduce(function(p, n) { p[n.a] = n; return p; }, {});
// n.a or n['a'] - primary key (in sample, [1,2,3...])
// "myMap[1].foo", "myMap[2].bar"
// or change KEY as your taste.
myMap = json.reduce(function(p, n) { p['k' + n.a] = n; return p; }, {});
// "myMap.k1.foo", "myMap.k2.bar"
// It's unique but if you want everything...
myMap = json.reduce(function(p, n) {
var k = 'k' + n.a;
if(p[k] !== undefined) { p[k] = n; }
else if(Array.isArray(p[k])) { p[k].push(n); }
else { p[k] = [p[k]] ; }
return p;
}, {});
}

Javascript: how to dynamically create nested objects INCLUDING ARRAYS using object names given by an array

Very similar to this question:
Javascript: how to dynamically create nested objects using object names given by an array
Instead of calling
assign(obj, keyPath, value)
example of usage of the previously answer:
var accountinfo = {}
assign(accountinfo, ["name", "addressinfo", "zipcode"], "90210");
That will output:
accountinfo = {name: "", addressinfo: {zipcode:"90210"}};
Now, I'd like to support arrays... in the above example, I'd like to support multiple addressinfo per account. I'd like to say:
assign(accountinfo, ["name", "addressinfo[1]", "zipcode"], "90210");
The result would be:
accountinfo = {name: "", addressinfo: [{},{zipcode:"90210"}]}
var regex = /\[([0-9]+)\]/ will show me the # inside the brackets, but I'm not sure how I'd have to iterate through each element in the array to make sure it exists (and create it if it doesn't).. and the difficult part, support this for each array element submitted as part of the function (I'd like to say :
assign(accountinfo, ["family", "name[3]", "addressinfo[1]", "zipcode"], "90210");
Edit:
Figured it out.
function assign(obj, keyPath, value) {
keyPath = keyPath.split(‘.’);
lastKeyIndex = keyPath.length - 1;
var re = /^(.+?)\[*(\d+)*\]*$/;
for (var i = 0; i < lastKeyIndex; i++) {
key = keyPath[i];
var ind;
var middle = re.exec(key);
key = middle[1];
ind = middle[2];
if (ind) {
if (!(obj[key]))
obj[key] = [];
if (!(obj[key][ind]))
obj[key][ind] = {};
}
if (!(key in obj))
obj[key] = {};
if (ind)
obj = obj[key][ind];
else
obj = obj[key];
}
obj[keyPath[lastKeyIndex]] = value;
}

How to get these key in JavaScript object

I have the following object:
var input = {
"document": {
"people":[
{"name":"Harry Potter","age":"18","gender":"Male"},
{"name":"hermione granger","age":"18","gender":"Female"}
]
}
}
I do like this :
_.each(result.document[people], function(item){
console.log(item);
//What should I do here ? or I come wrong way ?
});
And in item I get :
{name : 'Harry Potter', age : '18':, gender:'Male'}
{name : 'hermione grange', age : '18':, gender:'Female'}
I would like to get [name,age,gender]. What should I do?
If you think your values are dynamic use a function first
var input = {
"document": {
"people":[
{"name":"Harry Potter","age":"18","gender":"Male"},
{"name":"hermione granger","age":"18","gender":"Female"}
]
}
}
var func = function (one, two) {
var array = input[one][two];
var arr =[];
for (var i=0; i<array.length; i++){
arr = Object.keys(array[0]);
}
return arr;
}
func("document", "people"); // will return ["name", "age", "gender"]
Try this
var s = {name: "raul", age: "22", gender: "Male"}
var keys = [];
for(var k in s) keys.push(k);
Here keys array will return your keys ["name", "age", "gender"]
Something like this?
_.each(result.document[people], function(item) {
_.each(item, function(item, key) {
console.log(key);
});
});
_.each sends in a second key parameter to the callback function in the case of objects.
OK, Now I know you actually want the name of object, not the value. So I add another code for you.
I'm sorry I don't have time to explain now, but this code I wrote does the trick you need.
This shows the name of object:
root_obj=input.document.people[0];
tmp=[];
for(val in root_obj )
{
tmp.push(val);
}
console.log(tmp);
This shows the value of object:
root_obj=input.document.people;
for(obj in root_obj )
{
tmp=[];
for(val in root_obj[obj] )
{
tmp.push(root_obj[obj][val]);
}
console.log(tmp);
}
Here you go.. Final answer. (edited to return keys instead of values as per comment)
_.each(result.document[people], function(item){
//get keys as numerical array
var num_arr = [];
for (var key in item) {
num_arr.push( key );
}
console.log(num_arr); // should return ['name', 'age', 'gender']
});

In Javascript, how can I Rename/Renumber a set of properties?

This is one of those questions I'm ashamed to even ask, but I'm working with an external JSON source and I'm forced to do something ugly. So here goes...
I have 'dirty' Javascript object, with property names containing a number at their end:
{ "Friend1" : "Bob",
"Friend6" : "Fred",
"Friend632" : "Gonzo",
"FriendFinder1" : "Dolly",
"FriendFinder4294" : "Jan"
}
I'm trying to figure out a way to clean-up/"zero-index" these property names so the object would look like:
{ "Friend0" : "Bob",
"Friend1" : "Fred",
"Friend2" : "Gonzo",
"FriendFinder0" : "Dolly",
"FriendFinder1" : "Jan"
}
I'm referencing this indexOf/Regex code:
Is there a version of JavaScript's String.indexOf() that allows for regular expressions?
Any strategies you could recommend for doing this? I'll post where I'm at in a bit. Many thanks!
Take the "base" of a key and append items with a common base to an array using the original index. (This produces a sparse array.) Then stretch it out again by enumerating each item with a common base into a new key with 'base'+enumeratedindex.
The trick here is to use a method like forEach to enumerate the array--this will only visit assigned items in a sparse array, allowing you to determine the sort order just by using the original index-part of the key.
If you don't have access to forEach, you can accomplish a similar task by including the key in the array items. Instead of an intermediate array like this:
{Friend: [undefined, "Bob", undefined, undefined, undefined, undefined, "Fred"]}
You have one like this:
{Friend: [[6, 'Fred'],[1, 'Bob']]}
Then you sort the array and visit each item in a foreach loop, extracting the second item.
Here is code:
function rekey(obj) {
var rekey = /^(.*?)(\d+)$/;
var nestedobj = {}, newobj = {};
var key, basekeyrv, newkey, oldidx, newidx;
function basekey(key) {
return rekey.exec(key).splice(1);
}
for (key in obj) {
if (obj.hasOwnProperty(key)) {
basekeyrv = basekey(key);
newkey = basekeyrv[0];
oldidx = parseInt(basekeyrv[1], 10);
if (!nestedobj[newkey]) {
nestedobj[newkey] = [];
}
nestedobj[newkey][oldidx] = obj[key];
}
}
for (key in nestedobj) {
if (nestedobj.hasOwnProperty(key)) {
newidx = 0;
nestedobj[key].forEach(function(item){
newobj[key+newidx++] = item;
});
}
}
return newobj;
}
rekey({
"Friend1" : "Bob",
"Friend6" : "Fred",
"Friend632" : "Gonzo",
"FriendFinder1" : "Dolly",
"FriendFinder4294" : "Jan"
});
produces
{Friend0: "Bob",
Friend1: "Fred",
Friend2: "Gonzo",
FriendFinder0: "Dolly",
FriendFinder1: "Jan"}
Alternatively, without using forEach:
function rekey(obj) {
var rekey = /^(.*?)(\d+)$/;
var nestedobj = {}, newobj = {};
var key, basekeyrv, newkey, oldidx, newidx;
function basekey(key) {
return rekey.exec(key).splice(1);
}
for (key in obj) {
if (obj.hasOwnProperty(key)) {
basekeyrv = basekey(key);
newkey = basekeyrv[0];
oldidx = parseInt(basekeyrv[1], 10);
if (!nestedobj[newkey]) {
nestedobj[newkey] = [];
}
nestedobj[newkey].push([oldidx, obj[key]]);
}
}
for (key in nestedobj) {
if (nestedobj.hasOwnProperty(key)) {
nestedobj[key].sort();
for (newidx = 0; newidx < nestedobj[key].length; newidx++) {
newobj[key+newidx] = nestedobj[key][newidx][1];
}
}
}
return newobj;
}
Could you try doing the following:
{
friend: new Array(),
friendFinder: new Array()
}
then you can:
friend.push() - Add to array
var index = friend.indexOf("Bob") - find in array
friend.splice(index, 1) - remove from the array at index the 1 is for the number to remove.

Categories

Resources