I am getting the points from the multidimensional array using map, but I am getting array of array values.
I need the below format:
[36, 122, 25]
But I'm getting the multi array of points.
Below is my code and output
var data = [{
"name": "ramu",
"id": "719",
"gmail": "ramu#gmail.com",
"ph": 988989898,
"points": 36
},
{
"name": "ravi",
"id": "445",
"gmail": "ravi#gmail.com",
"ph": 4554545454,
"points": 122
},
{
"name": "karthik",
"id": "866",
"gmail": "karthik#gmail.com",
"ph": 2332233232,
"points": 25
}
]
var result = data.map(function(arr, count) {
return [arr.points];
});
console.log(result);
output is:
[[36], [122], [25]]
Try below code:
var data = [{"name":"ramu","id":"719","gmail":"ramu#gmail.com","ph":988989898,"points":36},
{"name":"ravi","id":"445","gmail":"ravi#gmail.com","ph":4554545454,"points":122},
{"name":"karthik","id":"866","gmail":"karthik#gmail.com","ph":2332233232,"points":25}]
var result = data.map(function(arr, count) { return arr.points;});
console.log(result);
var data = [{"name":"ramu","id":"719","gmail":"ramu#gmail.com","ph":988989898,"points":36},
{"name":"ravi","id":"445","gmail":"ravi#gmail.com","ph":4554545454,"points":122},
{"name":"karthik","id":"866","gmail":"karthik#gmail.com","ph":2332233232,"points":25}]
var result = data.map(function(arr, count) { return arr.points;});
console.log(result);
Remove the [] from return statement. It creates new array and push it to original.
In your code you are using [] that creates a new array using arr.points values. Remove the [] from return statement.
var data = [{
"name": "ramu",
"id": "719",
"gmail": "ramu#gmail.com",
"ph": 988989898,
"points": 36
},
{
"name": "ravi",
"id": "445",
"gmail": "ravi#gmail.com",
"ph": 4554545454,
"points": 122
},
{
"name": "karthik",
"id": "866",
"gmail": "karthik#gmail.com",
"ph": 2332233232,
"points": 25
}
]
var result = data.map(function(arr, count) {
return arr.points;
});
console.log(result);
The function is returning an array of single-element arrays because .map() returns an array, and within each iteration of .map() your function is returning [arr.points] (an element in an array). Change this to return arr.points and you're set.
Also, since you're merely returning a value in the .map() we can simplify the callback function to:
var result = data.map(arr => arr.points);
var data = [{
"name": "ramu",
"id": "719",
"gmail": "ramu#gmail.com",
"ph": 988989898,
"points": 36
},
{
"name": "ravi",
"id": "445",
"gmail": "ravi#gmail.com",
"ph": 4554545454,
"points": 122
},
{
"name": "karthik",
"id": "866",
"gmail": "karthik#gmail.com",
"ph": 2332233232,
"points": 25
}
]
var result = data.map(arr => arr.points);
console.log(result);
Related
const data = {
"games": [
{
"id": "828de9122149499183df39c6ae2dd3ab",
"developer_id": "885911",
"game_name": "Minecraft",
"first_release": "2011-18-11",
"website": "https://www.minecraft.net/en-us"
},
{
"id": "61ee6f196c58afc9c1f78831",
"developer_id": "810637",
"game_name": "Fortnite",
"first_release": "2017-21-07",
"website": "https://www.epicgames.com/fortnite/en-US/home"
},
],
"developers": [
{
"id": "885911",
"name": "Mojang Studios",
"country": "US",
"website": "http://www.mojang.com",
},
{
"id": "750245",
"name": "God of War",
"country": "SE",
"website": "https://sms.playstation.com",
},
] };
I have json data like this. I want to display data like if developer_id = 885911(from games array) then print id(from developers array) and if the both are same then I want to print the name.(Mojang studios) and so on like games website etc. How can I do that?
This sample code will show you the developer of each game, if it's found:
const data = {
"games": [
{
"id": "828de9122149499183df39c6ae2dd3ab",
"developer_id": "885911",
"game_name": "Minecraft",
"first_release": "2011-18-11",
"website": "https://www.minecraft.net/en-us"
},
{
"id": "61ee6f196c58afc9c1f78831",
"developer_id": "810637",
"game_name": "Fortnite",
"first_release": "2017-21-07",
"website": "https://www.epicgames.com/fortnite/en-US/home"
},
],
"developers": [
{
"id": "885911",
"name": "Mojang Studios",
"country": "US",
"website": "http://www.mojang.com",
},
{
"id": "750245",
"name": "God of War",
"country": "SE",
"website": "https://sms.playstation.com",
},
] };
const gameDevelopers = data.games.map(g => ({
game: g.game_name,
developer: data.developers.find(d => d.id === g.developer_id)?.name || "No matching developer found"
}));
console.log(gameDevelopers)
What did you exactly need? i don't understand . But you can get value Mojang Studios
console.log(data.developers[0].name)
If you need all developer id then you can use
console.log(data.developers.map(data=>{
console.log(data.id)
}))
If you need the id where name is Mojang Studios
console.log(data.developers.map(data=>{
if(data.name == "Mojang Studios"){
console.log(data.id)
}
}))
I have a JSON object like this, I wanna access the list array elements with key and value in postman.
{
"data": {
"total": 1,
"list": [
{
"id": 53,
"name": "Sonu",
"mobileNo": "6543213456",
"address": "Greeny Pathway",
"city": "NewYork",
"mode": "Weekly",
"duration": "15",
"qty": null
}
]
},
"success": true,
"message": ""
}
How to separate it as Key and Value in Javascript like,
Key: id,name,mobileNo,address,city,..
Value: 53,Sonu,6543213456,Greeny Pathway,NewYork,....
First remove comma from line : "qty": null, otherwise it will cause error in json parsing.
var resultJSON = `{
"data": {
"total": 1,
"list": [
{
"id": 53,
"name": "Sonu",
"mobileNo": "6543213456",
"address": "Greeny Pathway",
"city": "NewYork",
"mode": "Weekly",
"duration": "15",
"qty": null
}
]
},
"success": true,
"message": ""
}`;
var result = $.parseJSON(resultJSON);
var myList = result.data.list[0];
$.each(myList, function(k, v) {
//display the key and value pair
alert(k + ' is ' + v);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
you can use below codes:
const keys = Object.keys(jsonObject);
const values = Object.values(jsonObject);
But your JSON object is deep, you should flatten it and then use keys and values of Object to make them separate.
You can get using key and value separately in a array.
var a = {
"data": {
"total": 1,
"list": [
{
"id": 53,
"name": "Sonu",
"mobileNo": "6543213456",
"address": "Greeny Pathway",
"city": "NewYork",
"mode": "Weekly",
"duration": "15",
"qty": null,
}
]
},
"success": true,
"message": ""
}
var keyval = Object.keys(a.data.list[0])
console.log(keyval)
var values = Object.values(a.data.list[0])
console.log(values)
JSON objects are key value pair you cannot get the keys and values in object form as you desire but you can get both in form of arrays from this code
var key = []
var values = []
list.map(function(l){ keys = Object.getOwnPropertyNames(l);
keys.map(function(key) {values.push(l[key]);})})
Finally this works for me!(In Postman Script)
var resdata = JSON.parse(responseBody);
console.log(resdata);
key = Object.keys(resdata.data.list[0]);
console.log(key);
value =Object.values(resdata.data.list[0]);
console.log(value);
How can I display the members.Rating for "Jones, Jim"? I have tried the syntax: echo members[$temp].Rating but it doesn't work.
let $temp = "Jones, Jim";
var members = [
{ "Rating": "1500", "Name": "Williams, Bill"},
{ "Rating": "2000", "Name": "Smith, Paul" },
{ "Rating": "1000", "Name": "Jones, Jim" },
{ "Rating": "1750", "Name": "Reynolds, Beverly" } ]
Use Array.find:
let $temp = "Jones, Jim";
var members = [
{ "Rating": "1500", "Name": "Williams, Bill"},
{ "Rating": "2000", "Name": "Smith, Paul" },
{ "Rating": "1000", "Name": "Jones, Jim" },
{ "Rating": "1750", "Name": "Reynolds, Beverly" } ]
console.log(
members.find(x => x.Name === $temp).Rating
)
You can't directly reference a member of an array by value, only by index.
So, you'll have to use the find method, like so:
const tempMember = members.find(p => p.Name === $temp)
const tempMemberRating= tempMember && tempMember.Rating
Note that if find doesn't find the element you want, it will return undefined. This makes the mult-line approach necessary, as simply calling it all in one line could result in a TypeError. e.g.:
members.find(p => p.Name === "Johnson, Jimmy").Rating
Since find returns undefined here, you're attempting to reference undefined.Rating, which will throw an error.
Try this:
For (var i=0 ;i <members.length ;i++){
If ( members[i].Name == $temp ){
console.log (members[i].Raiting);
}
}
I am currently working on a REST API / website project, where my REST API has to return an array of objects from the server, via a response and using GSON to make a Json array out of the data. However, when trying to get values from the javascript array for the website, I keep getting undefined. This is the array:
var userArr =[
{
"0x1": {
"firstName": "Test1",
"lastName": "Test1",
"hobbies": [
{
"id": 1,
"name": "Fodbold",
"people": [
"0x1"
]
}
],
"id": 1,
"address": {
"id": 1,
"street": "Street1",
"cityInfo": {
"id": 1,
"zipCode": "0555",
"city": "Scanning"
},
"infoList": [
"0x1",
"0x2"
]
},
"phones": [
{
"id": 1,
"number": "123124",
"info": "0x1"
}
]
}
];
When I try to call userArr[0].firstName, I get an error saying that it's undefined, even though the data is there. This is from a get call, which I am doing in my javascript from my REST API, which sends back this specific array. I have tried looping through the array, with multiple objects inside, however I am unable to retrieve any info at all.
Your userArr is an array of objects which do not have firstName property. They have only one property named 0x1 for some reason. And this 0x1 property has firstName property.
You can access firstName of 0x1 property using this notation:
userArr[0]["0x1"].firstName
Here is the working demo:
var userArr = [{
"0x1": {
"firstName": "Test1",
"lastName": "Test1",
"hobbies": [{
"id": 1,
"name": "Fodbold",
"people": [
"0x1"
]
}],
"id": 1,
"address": {
"id": 1,
"street": "Street1",
"cityInfo": {
"id": 1,
"zipCode": "0555",
"city": "Scanning"
},
"infoList": [
"0x1",
"0x2"
]
},
"phones": [{
"id": 1,
"number": "123124",
"info": "0x1"
}]
}
}];
console.log(userArr[0]["0x1"].firstName);
By the way, there is a missing closing } bracket in the end of the array in your code.
I think if you write this code this way it is easy to understand and find the problem
var userArr =[
{
"0x1": {
"firstName": "Test1",
"lastName": "Test1",
"hobbies": [{"id": 1,"name": "Fodbold","people": ["0x1"]}],
"id": 1,
"address": {"id": 1,"street": "Street1","cityInfo": {"id": 1,"zipCode": "0555","city": "Scanning"},
"infoList": ["0x1","0x2"]},
"phones": [{"id": 1,"number": "123124","info": "0x1"}]
}
}
];
You also missing the last second bracket.
Then you could use this console.log(userArr[0]["0x1"].firstName);
try this one
userArr[0]["0x1"].firstName
Incase value "0x1" is dynamic, you can access it by using Object.keys(userArr[0])[0] to get the first key of the object.
Here is solution:
var userArr = [{
"0x1": {
"firstName": "Test1",
"lastName": "Test1",
"hobbies": [{
"id": 1,
"name": "Fodbold",
"people": [
"0x1"
]
}],
"id": 1,
"address": {
"id": 1,
"street": "Street1",
"cityInfo": {
"id": 1,
"zipCode": "0555",
"city": "Scanning"
},
"infoList": [
"0x1",
"0x2"
]
},
"phones": [{
"id": 1,
"number": "123124",
"info": "0x1"
}]
}
}];
console.log(userArr[0][Object.keys(userArr[0])[0]].firstName);
I have 2 json files,services.json and services_show.json.At page load am fetching the data from services.json and it working properly.On a button click,i need to fetch the contents from service_show.json and append to the services array but it does not work.
var beautyApp = angular.module('findbeauty', []);
beautyApp.controller('beautycntrl',function($scope,$http){
$http.get('http://localhost/Find-Beauty/media/services.json').success(function(data) {
$scope.services=data.services;
$scope.services1=data.services1;
});
$scope.Add = function(){
$http.get('http://localhost/Find-Beauty/media/services_show.json').success(function(data) {
console.log(angular.toJson(data.services));
$scope.services.push(data.services);
});
};
$scope.ViewMore = function(){
});
Services.json
{
"services":[
{
"name": "Arun",
"gender": "Damen",
"duration": "1.5 Stunden",
"price": "€65,00",
"imagepath": "media/images/prfilepic1.png",
"percentage": "90%"
},
],
"services1":[
{
"name": "Schnitt & Föhnen",
"gender": "Damen",
"duration": "1.5 Stunden",
"price": "€65,00",
"imagepath": "media/images/profilepic4.png",
"percentage": "25%"
},
]
}
service_show.json
{
"services":[
{
"name": "Schnitt & Föhnen",
"gender": "Damen",
"duration": "1.5 Stunden",
"price": "€65,00",
"imagepath": "media/images/profilepic4.png",
"percentage": "5%"
},
],
"services1":[
{
"name": "Schnitt & Föhnen",
"gender": "Damen",
"duration": "1.5 Stunden",
"price": "€65,00",
"imagepath": "media/images/prfilepic1.png",
"percentage": "50%"
},
]
}
How can i push the services_show.json data to $scope.services ? Any Help?
Array.prototype.push.apply() can be used for merging two arrays.
Merge the second array into the first one
$scope.services.push.apply($scope.services, data.services);
You need to push one item at a time, like
angular.forEach(data.services,function(item) {
$scope.services.push(item);
});
You also can use concat:
Here I have my own code: Please refer
$http.get('store/LoadAlbums/', {'iCategoryID': iCategoryID}).then(function (data) {
for (var i = 0; i < $scope.result.mainalbums.length; i++) {
if($scope.result.mainalbums[i].iCategoryID == iCategoryID){
$scope.result.mainalbums[i].albums = $scope.result.mainalbums[i].albums.concat(data.data.albums);
$scope.result.mainalbums[i].totalAlbumCategory = $scope.result.mainalbums[i].albums.concat(data.data.totalAlbumCategory);
$scope.result.mainalbums[i].loadmore = $scope.result.mainalbums[i].albums.concat(data.data.loadmore);
$scope.result.mainalbums[i].newpage = $scope.result.mainalbums[i].albums.concat(data.data.newpage);
}
} });