So I want to get the object by the id 1 in this object:
let users = {
'users': {
'user1': {
'id': '1',
'name': 'Brandon',
'DOB': '05/04/2000'
},
'user2': {
'id': '2',
'name': 'Jefferson',
'DOB': '05/19/2004'
}
}
}
and I want it to return the entire 'user1' array and log it, does anyone know how I could do this?
I looked all over stackoverflow, and docs, and couldn't find a way to do this. Could I get some help?
There are a few approaches, both of these should roughly achieve what you're looking for:
let users = {
'users': {
'user1': {
'id': '1',
'name': 'Brandon',
'DOB': '05/04/2000'
},
'user2': {
'id': '2',
'name': 'Jefferson',
'DOB': '05/19/2004'
}
}
}
const findUserById = (id) => {
const key = Object.keys(users.users).find(user => users.users[user].id === '1')
return users.users[key]
}
console.log(findUserById('1'))
let users = {
'users': {
'user1': {
'id': '1',
'name': 'Brandon',
'DOB': '05/04/2000'
},
'user2': {
'id': '2',
'name': 'Jefferson',
'DOB': '05/19/2004'
}
}
}
const findUserById = (id) => {
const [key, user] = Object.entries(users.users).find(([key, user]) => user.id === '1');
return user;
}
console.log(findUserById('1'))
While the answer by skovy is right and this is what you should be doing in an actual production setting, I would advise against applying it immediately in your situation.
Why? Your question shows that you first need to learn some basic principles any JavaScript programmer should have, that is:
How to iterate over contents of an object
The simplest method used to iterate over an object's keys is the for .. in loop. When iterating over an object's keys using the for .. in loop, the code inside the curly brackets will be executed once for every key of the object we are iterating.
let users = {
"user1": {
"id": 1
},
"user2": {
"id": 2
}
}
for (let key in users) {
console.log(key);
}
The above code will print:
user1
user2
Proceeding from that, it should be clear how to find the element we want:
let foundUser = null;
for (let key in users) {
if (users[key].id === 1) {
foundUser = users[key];
break;
}
}
// now found user is our user with id === 1 or null, if there was no such user
When not to do that
If you have a complex object which is a descendant of another object and don't want to iterate over inherited properties, you could instead get an array of current object's keys with Object.keys:
let users = {
"user1": {
"id": 1
},
"user2": {
"id": 2
}
}
const keys = Object.keys(users) // now this is an array containing just keys ['user1', 'user2'];
let foundUser = null;
// now you can iterate over the `keys` array using any method you like, e.g. normal for:
for (let i = 0; i < keys.length; i++) {
if (users[keys[i]].id === 1) {
foundUser = users[keys[i]];
break;
}
}
// or alternatively `for of`:
for (for key of keys) {
if (users[key].id === 1) {
foundUser = users[key];
break;
}
}
Other options
You could use Object.values to get an array containing all values of the object:
let users = {
"user1": {
"id": 1
},
"user2": {
"id": 2
}
}
const values = Object.values(users); // values: [{"id":1},{"id":2}]
You can now find the entry you want on your own:
let foundUser = null
for (let i = 0; i < values.length; i++) {
if (values[i].id === 1) {
foundUser = values[i];
break;
}
}
Or using the Array's find method:
let foundUser = values.find(user => user.id === 1);
// now foundUser contains the user with id === 1
Or, shorter and complete version:
let users = {
"user1": {
"id": 1
},
"user2": {
"id": 2
}
}
const foundUser = Object.values(users).find(user => user.id === 1);
// now foundUser is `{ id: 1 }`
Not a big fan of reinventing the wheel. We use object-scan for most of our data processing now. It's very handy when you can just use a tool for that kind of stuff. Just takes a moment to wrap your head around how to use it. Here is how it could answer your questions:
// const objectScan = require('object-scan');
const find = (id, data) => objectScan(['**.id'], {
abort: true,
rtn: 'parent',
filterFn: ({ value }) => value === id
})(data);
const users = { users: { user1: { id: '1', name: 'Brandon', DOB: '05/04/2000' }, user2: { id: '2', name: 'Jefferson', DOB: '05/19/2004' } } };
console.log(find('1', users));
// => { id: '1', name: 'Brandon', DOB: '05/04/2000' }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.8.0"></script>
Disclaimer: I'm the author of object-scan
A simple for loop would do it:
let users = {
'users': {
'user1': {
'id': '1',
'name': 'Brandon',
'DOB': '05/04/2000'
},
'user2': {
'id': '2',
'name': 'Jefferson',
'DOB': '05/19/2004'
}
}
}
let desiredUser = {};
Object.keys(users.users).forEach((oneUser) => {
if(users.users[oneUser].id === "1")
desiredUser = users.users[oneUser];
});
console.log(desiredUser);
You can also use reduce for this... as well as if you wanted to return the "entire" object, you could do:
let USER_LIST = {
'users': {
'user1': {
'id': '1',
'name': 'Brandon',
'DOB': '05/04/2000'
},
'user2': {
'id': '2',
'name': 'Jefferson',
'DOB': '05/19/2004'
}
}
}
function findUserById(id){
return Object.entries(USER_LIST.users).reduce((a, [user, userData]) => {
userData.id == id ? a[user] = userData : '';
return a;
}, {});
}
console.log(findUserById(1));
I agree with the Answers. just a short and simple way is to use .find() method
//--data returned-----//
data = [{"Id":22,"Title":"Developer"},{"Id":45,"Title":"Admin"}]
fs.readFile('db.json','utf8', function(err,data){
var obj = JSON.parse(data);
console.log(obj);
var foundItem = obj.find(o=>o.Id==id);
console.log(foundItem);
});
I've got an array of objects and I need a function that finds object an array by the objects property (id in example) and changes its other property (name in example). Currently my implementation looks like this:
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function (id, newName) {
arrayOfObjects.filter(function(obj) {return obj.id === id;}).name = newName;
};
console.log(JSON.stringify(arrayOfObjects)); // initial array logged
setNameById(2, 'Charlie');
console.log(JSON.stringify(arrayOfObjects)); // initial array logged
I understand that the problem is in changing the object that is returned by filter and not initial one, but I haven't found implementations that give access to initial object in such a situation, is it possible or do I need to rethink the steps that led me to this point.
Use this instead of filter. Filter produce new array.
arrayOfObjects.forEach(function(v){
if (v.id == id) {v.name = newName}
});
Use Array#forEach over Array#filter
Note that == or === should be used to compare, = will assign the value!
Array#forEach with condition should be enough just to update existing array of object.
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function(id, newName) {
var filtered = arrayOfObjects.filter(function(obj) {
return obj.id == id;
});
filtered.forEach(function(el) {
el.name = newName;
});
return filtered;
};
console.log(JSON.stringify(arrayOfObjects));
var filtered = setNameById(2, 'Charlie');
console.log(JSON.stringify(filtered));
Or use Array#map
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function(id, newName) {
return arrayOfObjects.filter(function(obj) {
return obj.id == id;
}).map(function(el) {
el.name = newName;
return el;
});
};
console.log(JSON.stringify(arrayOfObjects));
var filtered = setNameById(2, 'Charlie');
console.log(JSON.stringify(filtered));
Filter returns an array. So will have to use index in your approach.
Your Approach
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function(id, newName) {
arrayOfObjects.filter(function(obj) {
return obj.id = id;
})[0].name = newName;
};
console.log(JSON.stringify(arrayOfObjects)); // initial array logged
setNameById(2, 'Charlie');
console.log(JSON.stringify(arrayOfObjects));
array.find
If you are sure it will return only one value, use array.find
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function(id, newName) {
arrayOfObjects.find(function(obj) {
return obj.id = id;
}).name = newName;
};
console.log(JSON.stringify(arrayOfObjects)); // initial array logged
setNameById(2, 'Charlie');
console.log(JSON.stringify(arrayOfObjects));
array.forEach
If there can be more than 1 object with same search key (id in this case), use array.forEach instead of array.filter and then loop over filtered array
var arrayOfObjects = [{
id: 1,
name: 'Alpha'
}, {
id: 2,
name: 'Bravo'
}];
var setNameById = function(id, newName) {
arrayOfObjects.forEach(function(obj) {
if(obj.id = id) obj.name = newName;
});
};
console.log(JSON.stringify(arrayOfObjects)); // initial array logged
setNameById(2, 'Charlie');
console.log(JSON.stringify(arrayOfObjects));
This question already has an answer here:
How to merge objects using javascript
(1 answer)
Closed 8 years ago.
I would like to merge objects here and would like to convert to JSON object. I would like to create an object array and add objects in to it. In this example i would like to create ConsData object array and add each dataset in to it.
I would like the data to be like
[
{
"name":"aaa_aaaurf",
"region":"F&R",
"checkins":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,3],[9,0],[10,0],[11,0],[12,0]],
"teamsize":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,1],[9,0],[10,0],[11,0],[12,0]],
"Checkintimes":[[1,0],[2,0],[3,0],[4,184],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0]]
},
{
"name":"aaa_accessservices",
"region":"F&R",
"checkins":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,27],[12,12]],
"teamsize":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,11],[12,11]],
"Checkintimes":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,10],[12,12]]
}]
var ConsData = {};
var MergeData = {};
for(var x=0;x<dataset.length;x++)
{
repository = dataset[x].repository;
sbu = dataset[x].BusinessUnit
checkinsarray.push([index, dataset[x].AvgCheckinCount]);
teamsizearray.push([index, dataset[x].TeamSize]);
checkintimesarray.push([index, dataset[x].MeanBuildTimeHrs]);
ConsData["name"] = repository;
ConsData["region"] = sbu;
ConsData["checkins"] = checkinsarray;
ConsData["teamsize"] = teamsizearray;
ConsData["Checkintimes"] = checkintimesarray;
}
following is the data contained in dataset(fetched from csv file):
repository,month,year,MeanBuildTimeHrs,AvgCheckinCount,TeamSize,BusinessUnit
aaa_aaaurf,1,2013,0,0,0,Financial&Risk
aaa_aaaurf,2,2013,0,0,0,Financial&Risk
aaa_aaaurf,3,2013,0,0,0,Financial&Risk
aaa_aaaurf,4,2013,184,3,3,Financial&Risk
aaa_aaaurf,5,2013,0,0,0,Financial&Risk
aaa_aaaurf,6,2013,0,0,0,Financial&Risk
aaa_aaaurf,7,2013,0,0,0,Financial&Risk
aaa_aaaurf,8,2013,0,3,1,Financial&Risk
aaa_aaaurf,9,2013,0,0,0,Financial&Risk
aaa_aaaurf,10,2013,0,0,0,Financial&Risk
aaa_aaaurf,11,2013,0,0,0,Financial&Risk
aaa_aaaurf,12,2013,0,0,0,Financial&Risk
cCG_tzz,1,2013,5,3,100,Financial&Risk
cCG_tzz,2,2013,8,5,80,Financial&Risk
aCG_txz,1,2013,12,3,70,Financial&Risk
GCG_txz,1,2013,21,3,50,Financial&Risk
GCG_txz,2,2013,12,3,70,Financial&Risk
var dataArray = [], i;
for(i = 0; i < dataset.length; i++)
{
dataArray.push({
"name": dataset[x].repository,
"region": dataset[x].BusinessUnit,
"checkins": [index, dataset[x].AvgCheckinCount],
"teamsize": [index, dataset[x].TeamSize],
"Checkintimes": [index, dataset[x].MeanBuildTimeHrs]
});
}
console.log(JSON.stringify(dataArray));
You need to use a new object for every element in your array. Objects are stored by reference, and variables have function scope.
After having a look at your other questions, I recommend you to have a look at the JavaScript guide.
Can you use lodash?
var names = {
'characters': [
{ 'name': 'barney' },
{ 'name': 'fred' }
]
};
var ages = {
'characters': [
{ 'age': 36 },
{ 'age': 40 }
]
};
_.merge(names, ages);
// → { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] }
var food = {
'fruits': ['apple'],
'vegetables': ['beet']
};
var otherFood = {
'fruits': ['banana'],
'vegetables': ['carrot']
};
_.merge(food, otherFood, function(a, b) {
return _.isArray(a) ? a.concat(b) : undefined;
});
// → { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }