Create new array using JSON results with javascript [duplicate] - javascript

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)

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.$);

Reading Json data and using it in angular component

I am working on an angular application. I have to read data from json and process in two parts.
Read data from json using express: I have following Json in alphabetically sorted order.
{
"1": "Andy",
"2": "Billy"
"3": "Carry"
"4": "Doda"
}
Left hand side are Id's and Right one's are names. I have to read this data using express and access this data in my angular component.
In my angular component I have another array which is also sorted. That array is as follows:
[
{
"Name": "Andy",
"Id": "1"
"IncomingTime": "2020-06-19T11:02+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
},
{
"Name": "Billy",
"Id": "2"
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:17+00:00"
},
{
"Name": "Ciena",
"Id": 5
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
},
{
"Name": "Doda",
"Id": "4"
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
}
]
Once above Json data is available in component(JSON in point 1), I have to make a new array(or any data structure which can be sorted). In that new array I have to put data in such a way that alphabetically sorted "Name" coming from above Json should match with with data in sorted array. If Name is matched then content(Name, id, IncomingTime , OutgoingTime) is copied to new array for that particular "Name" which will also be sorted. If suppose While comparing names we came across a name for which data is not there in sorted array, we need to keep only Name in new array doesn't matter data is there or not. Like in above data for "Carry" data is not there in sorted array still in final array "Carry" should be there. How can I do that?
You can do it using Lodash. Lodash provides functions to achieve what you want. documentation is here.
install Lodash from here.
import Lodash import * as _ from 'lodash';
I think this is what you want
const arr = { 1: 'Andy', 2: 'Billy', 3: 'Carry', 4: 'Doda' };
const data = [
{
"Name": "Andy",
"Id": "1",
"IncomingTime": "2020-06-19T11:02+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
},
{
"Name": "Billy",
"Id": "2",
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:17+00:00"
},
{
"Name": "Ciena",
"Id": 5,
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
},
{
"Name": "Doda",
"Id": "4",
"IncomingTime": "2020-06-19T11:05+00:00",
"Outgoingtime": "2020-06-19T11:07+00:00"
}
];
const newArray = _.map(arr, item => {
const value = _.find(data, ['Name', item]);
const obj = value ? value : {Name: item};
return obj;
});
console.log(newArray);

Sort one array based on another array when second array is the key in first array. [duplicate]

This question already has answers here:
Sort array containing objects based on another array [duplicate]
(2 answers)
Closed 4 years ago.
Sort one array based on another array when second array is the key in first array.
Lets say I have a main array like below:
let mainArray = [{ "id": 24443, "name": "Apple" }, { "id": 20172, "name": "Banana" }, { "id": 187016, "name": "Cherry" }, { "id": 217632, "name": "Guava" }, { "id": 5221, "name": "Peach" }, { "id": 97568, "name": "Strawberry" }]
And I have id array like below:
let idArray = [ "24443", "20172", "5221", "187016", "217632", "97568" ]
Notice that second array contains values which are "id" property in the first array.
What are the best ways to sort the main array based on the order of occurrence of "key" in the second array?
If you are not absolutely after performance, here is a simple way of doing it:
mainArray.sort( (a,b) => idArray.indexOf(a.id.toString()) - idArray.indexOf(b.id.toString()));
Or for linear algorithm (parse both vectors only once):
// Array to Map
let map = mainArray.reduce( (acc, {id,name}) => Object.assign(acc, {[id] : name}), {});
idArray.map( id => ({id, name: map[id]}) )
I have created a function that does map and sorting:
This function will map through the array of keys and find that key in the mainArray and return it.
var mapAndSort = (mainArr, indexArr, key) => {
if (mainArr.length != indexArr.length) {
return null;
}
return indexArr.map(val => {
return mainArr.find(c => c[key] == val);
});
}
var mainArray = [{ "id": 24443, "name": "Apple" }, { "id": 20172, "name": "Banana" }, { "id": 187016, "name": "Cherry" }, { "id": 217632, "name": "Guava" }, { "id": 5221, "name": "Peach" }, { "id": 97568, "name": "Strawberry" }];
var idArray = [ "20172","24443", "5221", "217632", "97568", "187016"];
var result = mapAndSort(mainArray, idArray, 'id');
document.write(JSON.stringify(result));

Creating a JavaScript Object from JSON File [duplicate]

This question already has answers here:
Convert array of object to single object in javascript(Angularjs)
(3 answers)
How to convert an array of objects to object with key value pairs
(7 answers)
Reduce array of objects into object [duplicate]
(3 answers)
How to restructure form json data?
(2 answers)
Closed 5 years ago.
I have a json file like this below:
data.json:
[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]
Now, i want to turn this json into javascript object like this format:
JavaScript Object:
{
key0: val0,
key1: val1,
key2: val2,
key3: val3
}
I have tried to find a solution with the functions (like each, map, ...), but i couldn't find a solution. I am not just looking for a solution, but the most efficent one.
Do you have an idea?
reduce is a good option here, as were reducing from an array to an object.
var json = `[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]`;
var obj = JSON.parse(json).reduce((a,v) => { a[v.key] = v.value; return a; }, {});
console.log(obj);

Categories