Get an adjacent value in JSON array if value is found [duplicate] - javascript

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 2 years ago.
If I have this type of object returned in javascript (as a result of $get)
[
{
"id": "1",
"username": "john"
},
{
"id": "2",
"username": "bill"
}
]
How would I search for the username "john" and if it exists return his ID
Thanks
i.e.
$.get( baseURL + "users/")
.done(function( data ) {
var usernames = data.results;
});

Use find():
var array =[
{
"id": "1",
"username": "john"
},
{
"id": "2",
"username": "bill"
}
];
var id = array.find(k=>k.username=='john')?.id;
console.log(id);

Related

Javascript - convert array to stringify format [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Serializing an object to JSON
(4 answers)
Closed 4 months ago.
I have this array:
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
And i need to convert it to this stringified format:
"{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
I have made several attempts without luck.
My last attempt was this one:
array = array.map((p) => {
return Object.values(p).join(':').replace(/\[/g, '{').replace(/]/g, '}').toString();
}),
Any solutions?
Simply make a empty object, iterate over your array to populate that object, then JSON.stringify(obj) to get your result.
Like this:-
var obj = {};
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
for(let i of array) {
obj[i.name] = i.value;
}
const str = JSON.stringify(obj);
console.log(str);
/*
output : "{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
*/

I have data in an array and am looking to strip out one part of it [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I would like to have an output of an array number_list = [1, 4]. What
I have overwrites my array. Any Ideas? Thanks
data = [{"key":"50", "name": "James", "$": "1"},
{"key":"20", "name": "George", "$": "4"}]
$.each(data, function(){
number = this.$
var number_list = number.push(this.$)
})
try:
const data = [{"key":"50", "name": "James", "$": "1"},
{"key":"20", "name": "George", "$": "4"}]
const number_list = data.map(i => i.$);

Create new array using JSON results with javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Hello I have the following JSON result. Its a small snippet from a large JSON file. Theres hundreds of entries.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
I would like a way to loop through this data using javascript to create a new array stored in a variable that pulls just the ids. So the new array would look like this:
data = [1, 2, 3];
If theres 1000 entries, then the array would go from 1-1000.
Thanks!
Very simple just use array.map.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
let idArray = jsonResult.map(element => element["id"]);
console.log(idArray)

string replace array map [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 6 years ago.
I have a hundreds of user in the list, below is just an example list :
var userList = [
{
"FullName": "test1",
"UserName": "test1",
"Email": "test1#test.com"
},
{
"FullName": "test2",
"UserName": "test2",
"Email": "test2#test.com"
}
];
var userStr = "test1 is doing a test2";
userList.map((user) => {
userStr.replace(RegExp(user.FullName,"gi"), user.Email)
})
console.log(userStr);
The test string return is still the same and is not replace with email. Is the code I do optimize?
You need to update the variable with the returned value.
var userList = [{
"FullName": "test1",
"UserName": "test1",
"Email": "test1#test.com"
}, {
"FullName": "test2",
"UserName": "test2",
"Email": "test2#test.com"
}];
var userStr = "test1 is doing a test2";
userList.forEach((user) => userStr = userStr.replace(RegExp(user.FullName, "gi"), user.Email))
console.log(userStr);

How to get names of nested Json objects with javascript [duplicate]

This question already has answers here:
Getting the object's property name [duplicate]
(13 answers)
Closed 7 years ago.
Sometimes there are a few values in JSON which are not present as name value pairs but as only names and then their properties and below for example in the JSON below objectOne, ObjectTwo and objectThree. The problem is that there names keep on changing how can I extract them if I don’t know in advance what these names are going to be? But the data structure will be same
{
"Number of objects": 3,
"Devices": {
"objectOne": {
"name": "10",
"name1": "50"
},
"objectTwo": {
"name": "20",
"name1": "30"
},
"objectThree": {
"name": "40",
"name1": "80"
}
}
}
You can try to use Object.keys method.
Sample :
var yourJson = {
"Number of objects": 3,
"Devices": {
"objectOne": {
"name": "10",
"name1": "50"
},
"objectTwo": {
"name": "20",
"name1": "30"
},
"objectThree": {
"name": "40",
"name1": "80"
}
}
}
var keys = Object.keys(yourJson.Devices); // Array with "objectOne", "objectTwo" and "objectThree"
UPDATE :
Then you can access to objectTwo this way :
var objectTwo = yourJson.Devices[keys[1]];
If you need to iterate through all, this is better :
for (var key in keys) {
// key = "objectOne", then "objectTwo", then "objectThree"
var objectN = yourJson.Devices[key]; // the device object
}

Categories