i want fetch 2 data and push array, then merge this two data, who can help me?
My first data example;
{
"experts": [
{
"id": 1,
"name": "XXXXX",
"integration_id": "1",
},
{
"id": 243,
"name": "YYYY",
"integration_id": "2",
},] }
My Second data example https://xxx/api/?staff={integration_id}
{
"uzmanlar": [
{
"id": "1",
"ad_soyad": "xxx",
"pic": "117-k-xxx.jpg",
}
],
}
i want foreach first data array to second data merge. i want print picture screen
const arr1 = res.exprts;
const arr2 = res.uzmanlar;
Array.prototype.push.apply(arr1, arr2);
console.log(arr1) // final merge result would be in arr1
you can try this...
You can achieve this using the ... Spread Syntax:
const array = [...obj1["experts"], ...obj1["uzmanlar"]]
This creates a new array including all elements of the first and second array.
Related
This is the json data I have. I need the sum of 'homescorepoints' + 'homeframepointsadj' and/or
'awayscorepoints' + 'awayframepointsadj'...
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
I understand the basic array. Reduce for adding multiple occurrences of say "awayscore", but I'm have a mental block with adding two separate objects values together.
Assuming that you wanted to sum values from this example object: .reduce() accept arrays so, you can use Object.values() method for your object to get the array and than use .reduce() like this:
const jsonData = {
"512830": {
"compname": "VNEA Vegas League",
"grade": "",
"hometeamlabel": "Pool Tang Clan",
"homeshortlabel": "Pool Tang Clan",
"awayteamlabel": "All Shades",
"awayshortlabel": "All Shades",
"homescore": 11,
"homescorepoints": "187",
"homeframepointsadj": "5",
"awayscore": 14,
"awayscorepoints": "178",
"awayframepointsadj": "0",
}
};
const calculateScore = (data, type) =>
Object.values(data).reduce((acc, curr) =>
acc + parseInt(curr[`${type}scorepoints`]) + parseInt(curr[`${type}framepointsadj`]), 0);
const homeScore = calculateScore(jsonData, 'home');
const awayScore = calculateScore(jsonData, 'away');
console.log(homeScore);
console.log(awayScore);
I'm using Nodejs with Mongoose package.
Given I've something like this:-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat"
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse"
}
]
}
]
I want to get only the data of Jerry in pets array using it's _id (result shown below)
{
"_id": 2,
"name": "Jerry",
"category": "mouse"
}
Can I get it without needing to specify the _id of person 1 when using $elemMatch? Right now I code like this:-
const pet = People.find(
{ "_id": "1"}, // specifying 'person 1 _id' first
{ pets: { $elemMatch: { _id: 2 } } } // using 'elemMatch' to get 'pet' with '_id' of '2'
)
And it gave me what I want like I've shown you above. But is there any other way I can do this without needing to specify the _id of it's parent first (in this case, the _id of the people array)
Assuming nested array's _id's are unique you can filter by nested array elements directly:
const pet = People.find(
{ "pets._id": 2 },
{ pets: { $elemMatch: { _id: 2 } } }
)
I know how to operate on array of objects but never had the necessity to push one array data into another. I need to push an array of objects into another array with only 2 fields of the object. Right now my object format is somewhat like this
data: [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
I want to push this into another array but I want only courseid and name in the object. I've read that we need to initialise the object in the constructor, use slice() and a few other functions and then push but I don't know how can I do it for mine since I need to push one array data into another.Kindly someone help me in this regard.
You're looking for the array map() method:
const newArray = array.map(o => {
return { name: o.name, courseid: o.courseid };
});
Try this:
let data = [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
let other = []; // your other array...
data.map(item => {
return {
courseid: item.courseid,
name: item.name
}
}).forEach(item => other.push(item));
console.log(JSON.stringify(other))
// => [{"courseid":"1","name":"Btech"},{"courseid":"2","name":"BCom"}]
You can simply do it like this.
//assign your array of object to variable
var youArray:Array<any>= [{
"name": "Btech",
"courseid": "1",
"courserating": 5,
"points": "100",
"type": "computers"
},
{
"name": "BCom",
"courseid": "2",
"courserating": 5,
"points": "100",
"type": "computers"
}];
var resultArray:Array<any>=[] //empty array which we are going to push our selected items, always define types
youArray.forEach(i=>{
resultArray.push(
{
"name":i.name,
"courseid":i.courseid
});
});
console.log(resultArray)
if you still have doubts about this.please follow this url
Map to a returning JSON data, subscribe it to an existing array or an empty one. #typescript
let pictimeline= [];
var timeline = this.picService.fetchtimeline(this.limit)
.map((data : Response) => data.json())
.subscribe(pictimeline=> this.pictimeline = pictimeline);
console.log(this.pictimeline);
I use this eample link to customize.There are json array and create bar charts according to array.
Please guide me, this is my json format:
{ "server1": [
{
"mount": "test1",
"value": 207
},
{
"mount": "test2",
"value": 20.07
}
],
"server2": [
{
"mount": "test1",
"value": 45
},
{
"mount": "test2",
"value": 0.04
}
]
}
I want to create 2 separate bar chart.
you can use loop for this
$.getJSON("./data/new.json", function(data){
for(i in data){
alert(i);
data1 = data[i];
after looping using your example this way link
pass value like data array and loop it!
d3.json("./data/new.json", function(error,data1){
for(r in data1){
like this you can implement this
Convert this json to two list using javascript or jquery whichever you like
t1 = [207, 45]
t2 = [20.07, 0.04]
now replace data (d) in the http://bl.ocks.org/mbostock/3885304 with t1 for graph 1 and with t2 for graph 2
it will work for sure
I have a collection like the following:-
{
_id: 5,
"org_name": "abc",
"items": [
{
"item_id": "10",
"data": [
// Values goes here
]
},
{
"item_id": "11",
"data": [
// Values goes here
]
}
]
},
// Another sub document
{
_id: 6,
"org_name": "sony",
"items": [
{
"item_id": "10",
"data": [
// Values goes here
]
},
{
"item_id": "11",
"data": [
// Values goes here
]
}
]
}
Each sub document corresponds to individual organizations and each organization has an array of items in them.
What I need is to get the select individual elements from the items array, by providing item_id.
I already tried this:-
db.organizations.find({"_id": 5}, {items: {$elemMatch: {"item_id": {$in: ["10", "11"]}}}})
But it is returning either the item list with *item_id* "10" OR the item list with *item_id* "11".
What I need is is the get values for both item_id 10 and 11 for the organization "abc". Please help.
update2:
db.organizations.aggregate([
// you can remove this to return all your data
{$match:{_id:5}},
// unwind array of items
{$unwind:"$items"},
// filter out all items not in 10, 11
{$match:{"items.item_id":{"$in":["10", "11"]}}},
// aggregate again into array
{$group:{_id:"$_id", "items":{$push:"$items"}}}
])
update:
db.organizations.find({
"_id": 5,
items: {$elemMatch: {"item_id": {$in: ["10", "11"]}}}
})
old Looks like you need aggregation framework, particularly $unwind operator:
db.organizations.aggregate([
{$match:{_id:5}}, // you can remove this to return all your data
{$unwind:"$items"}
])