Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 24 days ago.
Improve this question
I'm trying restructure an array and build another one. Tried to use filter, map, but couldn't achieved that. There are some factors in array and it's ratings (the 5 elements of object from the beginning). I should collect them in another array. I have an array like this.
[{
"carryRating": 21,
"distanceRating": 72,
"obHavoYogingarchilikQorVaHKRating": 74,
"obHavoQuruqRating": 40,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 32,
"id": 1,
"direction": "A",
"distance": "90.55",
"carry": "365",
"obHavoQuruq": "50",
"yetkazibBeruvchiningMoliyaviyAhvoli": "56",
"obHavoYogingarchilikQorVaHK": "58"
}, {
"carryRating": 35,
"distanceRating": 74,
"obHavoYogingarchilikQorVaHKRating": 71,
"obHavoQuruqRating": 29,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 55,
"id": 2,
"direction": "B",
"distance": "82.46",
"carry": "589",
"obHavoQuruq": "36",
"yetkazibBeruvchiningMoliyaviyAhvoli": "98",
"obHavoYogingarchilikQorVaHK": "65"
}, {
"carryRating": 19,
"distanceRating": 76,
"obHavoYogingarchilikQorVaHKRating": 90,
"obHavoQuruqRating": 17,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 11,
"id": 3,
"direction": "C",
"distance": "76.16",
"carry": "326",
"obHavoQuruq": "21",
"yetkazibBeruvchiningMoliyaviyAhvoli": "20",
"obHavoYogingarchilikQorVaHK": "23"
}, {
"carryRating": 25,
"distanceRating": 78,
"obHavoYogingarchilikQorVaHKRating": 65,
"obHavoQuruqRating": 14,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 2,
"id": 4,
"direction": "D",
"distance": "72.11",
"carry": "421",
"obHavoQuruq": "17",
"yetkazibBeruvchiningMoliyaviyAhvoli": "3",
"obHavoYogingarchilikQorVaHK": "78"
}]
I want to restructure it like this:
const newData = [
{
factor: "carry",
firstElementOfArray: 21, // it's carryRating
secondElementOfArray: 35,
thirdElementOfArray: 19,
fourthElementOfArray: 25
},
{
factor: "distance",
firstElementOfArray: 72, // it's distanceRating
secondElementOfArray: 74,
thirdElementOfArray: 76,
fourthElementOfArray: 78
},
{
factor: "obHavoQuruq",
firstElementOfArray: 40, // it's obHavoQuruqRating
secondElementOfArray: 29,
thirdElementOfArray: 17,
fourthElementOfArray: 14
},
{
factor: "yetkazibBeruvchiningMoliyaviyAhvoli",
firstElementOfArray: 32, // it's yetkazibBeruvchiningMoliyaviyAhvoliRating
secondElementOfArray: 55,
thirdElementOfArray: 11,
fourthElementOfArray: 2
},
{
factor: "obHavoYogingarchilikQorVaHK",
firstElementOfArray: 74, // it's obHavoYogingarchilikQorVaHKRating
secondElementOfArray: 71,
thirdElementOfArray: 90,
fourthElementOfArray: 65
},
]
How can I do that?
I'd solve it by mapping keys with properties, and iterating over them.
This solution does require you to have a fixed number of items in the input array, but it could be expanded with some kind of "translation" from [x] to 'x-thElementOfArray' if needed as well.
let input = [{
"carryRating": 21,
"distanceRating": 72,
"obHavoYogingarchilikQorVaHKRating": 74,
"obHavoQuruqRating": 40,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 32,
"id": 1,
"direction": "A",
"distance": "90.55",
"carry": "365",
"obHavoQuruq": "50",
"yetkazibBeruvchiningMoliyaviyAhvoli": "56",
"obHavoYogingarchilikQorVaHK": "58"
}, {
"carryRating": 35,
"distanceRating": 74,
"obHavoYogingarchilikQorVaHKRating": 71,
"obHavoQuruqRating": 29,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 55,
"id": 2,
"direction": "B",
"distance": "82.46",
"carry": "589",
"obHavoQuruq": "36",
"yetkazibBeruvchiningMoliyaviyAhvoli": "98",
"obHavoYogingarchilikQorVaHK": "65"
}, {
"carryRating": 19,
"distanceRating": 76,
"obHavoYogingarchilikQorVaHKRating": 90,
"obHavoQuruqRating": 17,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 11,
"id": 3,
"direction": "C",
"distance": "76.16",
"carry": "326",
"obHavoQuruq": "21",
"yetkazibBeruvchiningMoliyaviyAhvoli": "20",
"obHavoYogingarchilikQorVaHK": "23"
}, {
"carryRating": 25,
"distanceRating": 78,
"obHavoYogingarchilikQorVaHKRating": 65,
"obHavoQuruqRating": 14,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 2,
"id": 4,
"direction": "D",
"distance": "72.11",
"carry": "421",
"obHavoQuruq": "17",
"yetkazibBeruvchiningMoliyaviyAhvoli": "3",
"obHavoYogingarchilikQorVaHK": "78"
}];
let propertyMapping = {
carry: "carryRating",
distance: "distanceRating",
obHavoQuruq: "obHavoQuruqRating",
yetkazibBeruvchiningMoliyaviyAhvoli: "yetkazibBeruvchiningMoliyaviyAhvoliRating",
obHavoYogingarchilikQorVaHK: "obHavoYogingarchilikQorVaHKRating"
};
let output = Object.keys(propertyMapping).map(key => ({
factor: key,
firstElementOfArray: input[0][propertyMapping[key]],
secondElementOfArray: input[1][propertyMapping[key]],
thirdElementOfArray: input[2][propertyMapping[key]],
fourthElementOfArray: input[3][propertyMapping[key]]
}));
console.log(output);
Update
Alternatively, as you added that the source data will not always contain 4 items, it would be better to use a different output format (as proposed in the comments as well).
For example:
let output = Object.keys(propertyMapping).map(key => ({
factor: key,
items: input.map(it => it[propertyMapping[key]])
}));
or
let output = Object.keys(propertyMapping).reduce((prev, key) => ({ ...prev,
[key]: input.map(it => it[propertyMapping[key]])
}), {})
Related
In the below sentence In the word Asteroid A at 0 and d at 7 position after that space is counted as 8. just for a clear picture I have lot of space below.
// 0-7, 9-10, 12, 14-18, 20-26, 28-31, 33-37, 39-41, 43-46
// Asteroid is a rocky objects that orbit the Sun
Now, I have an object with word a rocky at 12 - 19 here 19th is blank space.
{
"start_offset": 12,
"end": 19,
"text": "a rocky",
"entity_type": "adjective",
},
Now, I should find all the words which falls between this start and end and push that into the above object under a key splits like below.
{
"start_offset": 12,
"end": 19,
"text": "a rocky",
"entity_type": "adjective",
"splits": [
{
"start_offset": 14,
"end": 19,
"text": "rocky",
"entity_type": "adjective",
},
]
},
This Iteration I need to do n number of times and finally should group all the elements as given in the output.
Right now I have tried something like below with close results but still need lot of improvement. can anyone guide me please
const res = arr.reduce((pv, cv) => {
const [{ start_offset, end }] = arr
.filter((s) => (s.start_offset <= cv.start_offset) && (s.end >= cv.end))
.sort((s1, s2) => (s2.end - s2.start_offset) - (s1.end - s1.start_offset));
const hash = `${start_offset}-${end}`;
pv[hash] = pv[hash]
? { ...pv[hash], splits: [...pv[hash].splits, cv] }
: { start_offset, end, splits: [cv] };
return pv;
}, {});
const result = Object.values(res);
console.log(result)
Given Input:
let arr = [
{
"start_offset": 0,
"end": 38,
"text": "Asteroid is a rocky objects that orbit",
"entity_type": "adjective",
},
{
"start_offset": 12,
"end": 19,
"text": "a rocky",
"entity_type": "adjective",
},
{
"start_offset": 14,
"end": 27,
"text": "rocky objects",
"entity_type": "adjective",
},
{
"start_offset": 20,
"end": 32,
"text": "objects that",
"entity_type": "adjective",
},
{
"start_offset": 14,
"end": 19,
"text": "rocky",
"entity_type": "adjective",
},
{
"start_offset": 20,
"end": 27,
"text": "objects",
"entity_type": "adjective",
},
{
"start_offset": 33,
"end": 47,
"text": "orbit the Sun",
"entity_type": "adjective",
},
{
"start_offset": 43,
"end": 47,
"text": "Sun",
"entity_type": "adjective",
}
]
Expected output:
let output = [
{
"start_offset": 0,
"end": 38,
"text": "Asteroid is a rocky objects that orbit",
"entity_type": "adjective",
"splits": [
{
"start_offset": 12,
"end": 19,
"text": "a rocky",
"entity_type": "adjective",
"splits": [
{
"start_offset": 14,
"end": 19,
"text": "rocky",
"entity_type": "adjective",
},
]
},
{
"start_offset": 14,
"end": 27,
"text": "rocky objects",
"entity_type": "adjective",
"splits": [
{
"start_offset": 20,
"end": 27,
"text": "objects",
"entity_type": "adjective",
},
]
},
{
"start_offset": 20,
"end": 32,
"text": "objects that",
"entity_type": "adjective",
},
]
},
{
"start_offset": 33,
"end": 47,
"text": "orbit the Sun",
"entity_type": "adjective",
},
{
"start_offset": 43,
"end": 47,
"text": "Sun",
"entity_type": "adjective",
}
]
You could sort the array in advance and reduce the array by looking to start and end ranges for smaller levels until a sub level is found.
const
data = [{ start_offset: 0, end: 38, text: "Asteroid is a rocky objects that orbit", entity_type: "adjective" }, { start_offset: 12, end: 19, text: "a rocky", entity_type: "adjective" }, { start_offset: 14, end: 27, text: "rocky objects", entity_type: "adjective" }, { start_offset: 20, end: 32, text: "objects that", entity_type: "adjective" }, { start_offset: 14, end: 19, text: "rocky", entity_type: "adjective" }, { start_offset: 20, end: 27, text: "objects", entity_type: "adjective" }, { start_offset: 33, end: 47, text: "orbit the Sun", entity_type: "adjective" }, { start_offset: 43, end: 47, text: "Sun", entity_type: "adjective" }],
result = data
.sort((a, b) => a.start_offset - b.start_offset || b.end - a.end)
.reduce((r, { ...o }) => {
let temp,
child = { split: r };
do {
temp = child;
temp.split = temp.split || [];
child = temp.split.find(q => q.start_offset <= o.start_offset && q.end >= o.end);
} while (child)
temp.split.push(o);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need help in merging the values of the objects in an array if the id is the same as the warehouse_item_id. In my case, there is a two objects which should be merge id: 191 and id: 52, because id 52 has warehouse_item_id of 191.
Please see the response and the code below.
CODE
const yeah = yes.filter(a => a.id === a.warehouse_item_id);
JSON Response
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
EXPECTED OUTPUT
[
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
> EXPECTED OUTPUT
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
}
]
Here is the steps which you need to follow.
Create a new array for merged objects
Iterate the array
Find the item which you're looking for
merge with the existing object to the filtered object
append to new object
done.
let json = [{"id":191,"warehouse_id":24,"ingredient_id":65,"expiration_date":"2019-07-31","available_stocks":7,"ingredient":{"id":65,"name":"erg","SKU":"1000064","default_unit":{"id":26,"name":"Milliliter"},"purchase_price":50}},{"id":192,"warehouse_id":24,"ingredient_id":66,"expiration_date":"2019-09-18","available_stocks":33994,"ingredient":{"id":66,"name":"gvf","SKU":"1000065","default_unit":{"id":27,"name":"Gram"},"purchase_price":60}},{"id":193,"warehouse_id":24,"ingredient_id":67,"expiration_date":"2019-09-19","available_stocks":43996,"ingredient":{"id":67,"name":"fwefe","SKU":"1000066","default_unit":{"id":26,"name":"Milliliter"},"purchase_price":70}},{"id":52,"outlet_item_id":null,"warehouse_item_id":191,"ingredient_id":65,"quantity":7,"total_in_lowest":0,"stock_on_hand":0,"adjustment_price":0,"soh_total_in_lowest":0,"unit_price":50,"difference":0,"difference_in_lowest":0}];
// new array after merging the objects
let desiredArray = [];
// iterate the array
let processedWarehouseItems = [];
for (let i = 0; i < json.length; i++) {
let newObj = json[i]; // assign the each object if no filtered found in case
// find the object based on your filter condition
let fiteredObj = json.filter(
e => e.id != json[i].id && e.warehouse_item_id === json[i].id
);
if (fiteredObj && fiteredObj.length) {
// if object found
processedWarehouseItems.push(fiteredObj[0].warehouse_item_id);
newObj = { ...json[i], ...fiteredObj[0]
}; // merge the objects and creates a new one
}
// check if the item is already merged in?
if (processedWarehouseItems.indexOf(json[i].warehouse_item_id) === -1) {
desiredArray.push(newObj); // push to the new array
}
}
console.log(desiredArray)
I am creating a little React application about Pokemons. I have in DB informations about all of them (about 900+).
They all contain an id field, which is an integer from 1 to 900+.
But the problem is when I do a request like this :
firebase.database().ref(`mydb`).orderByChild('id').startAt(1).limitToFirst(limit).once('value')
The results are not correct: I have an id array like this: [9,1, 10, 6, 4]
Am I doing something wrong ?
Edit:
I add the result I got when I perform the request I wrote above, I added a custom_id field containing ids as strings, but I stille have an unordered result:
[
{
"base_experience": 239,
"custom_id": "009",
"height": 16,
"id": 9,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/9/encounters",
"name": "blastoise",
"order": 12,
"weight": 855
},
{
"base_experience": 64,
"custom_id": "001",
"height": 7,
"id": 1,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/1/encounters",
"name": "bulbasaur",
"order": 1,
"weight": 69
},
{
"base_experience": 39,
"custom_id": "010",
"height": 3,
"id": 10,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/10/encounters",
"name": "caterpie",
"order": 14,
"weight": 29
},
{
"base_experience": 240,
"custom_id": "006",
"height": 17,
"id": 6,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/6/encounters",
"name": "charizard",
"order": 7,
"weight": 905
},
{
"base_experience": 62,
"custom_id": "004",
"height": 6,
"id": 4,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/4/encounters",
"name": "charmander",
"order": 5,
"weight": 85
},
{
"base_experience": 142,
"custom_id": "005",
"height": 11,
"id": 5,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/5/encounters",
"name": "charmeleon",
"order": 6,
"weight": 190
},
{
"base_experience": 142,
"custom_id": "002",
"height": 10,
"id": 2,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/2/encounters",
"name": "ivysaur",
"order": 2,
"weight": 130
},
{
"base_experience": 63,
"custom_id": "007",
"height": 5,
"id": 7,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/7/encounters",
"name": "squirtle",
"order": 10,
"weight": 90
},
{
"base_experience": 236,
"custom_id": "003",
"height": 20,
"id": 3,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/3/encounters",
"name": "venusaur",
"order": 3,
"weight": 1000
},
{
"base_experience": 142,
"custom_id": "008",
"height": 10,
"id": 8,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/8/encounters",
"name": "wartortle",
"order": 11,
"weight": 225
}
]
You've not attached your database image, but with the order one thing is sure that these keys in your database are strings.
And when you order string data, it is ordered lexicographically.
So for numbers, this is the normal order:
1
9
10
But for strings, this is the normal order:
"9"
"1"
"10"
I don't think there is any operator in Firebase (nor in most other databases) to change this behaviour.
Instead, you will have to modify the data to get the behavior you want. So: store values that are in the order you need them when sorted lexicographically.
For numbers you can accomplish that by padding them with zeroes:
"001"
"009"
"010"
EDIT:
Now after Json, these values are stored in id field so the above thing does not apply to this.
You may however use a code like this, to do what you're trying:
mDatabase
.child("main_child")
.orderByChild("id")
.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
System.out.println(child.getKey());
}
}
...
Any help is very much appreciated.
Basically I am using an api from mashape, but I'm a bit of a newbie to JSON files.
What I want to do is create a load of jquery variables for each teams total points.
Bare in mind that the teams change position in the JSON file depending on their position in the table.
Below is my jquery code so far (without the auth code) and the JSON file.
$.ajax({
url: 'https://heisenbug-premier-league-live-scores-v1.p.mashape.com/api/premierleague/table',
type: 'GET',
data: {},
dataType: 'json',
success: function(data) {
$(data.records).each(function(index, value) {
});
console.dir((data.source));
},
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization",
"Auth Code");
}
});
And the JSON file.
{
"records": [
{
"team": "Manchester City",
"played": 10,
"win": 8,
"draw": 0,
"loss": 2,
"goalsFor": 29,
"goalsAgainst": 12,
"points": 24
},
{
"team": "Arsenal",
"played": 10,
"win": 7,
"draw": 2,
"loss": 1,
"goalsFor": 16,
"goalsAgainst": 6,
"points": 23
},
{
"team": "Tottenham",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 18,
"goalsAgainst": 7,
"points": 19
},
{
"team": "Leicester",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 16,
"goalsAgainst": 13,
"points": 19
},
{
"team": "Manchester United",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 12,
"goalsAgainst": 4,
"points": 19
},
{
"team": "West Ham",
"played": 10,
"win": 4,
"draw": 4,
"loss": 2,
"goalsFor": 16,
"goalsAgainst": 12,
"points": 16
},
{
"team": "Liverpool",
"played": 9,
"win": 4,
"draw": 3,
"loss": 2,
"goalsFor": 11,
"goalsAgainst": 11,
"points": 15
},
{
"team": "Norwich",
"played": 10,
"win": 4,
"draw": 3,
"loss": 3,
"goalsFor": 12,
"goalsAgainst": 10,
"points": 15
},
{
"team": "Southampton",
"played": 10,
"win": 4,
"draw": 2,
"loss": 4,
"goalsFor": 17,
"goalsAgainst": 13,
"points": 14
},
{
"team": "Chelsea",
"played": 10,
"win": 4,
"draw": 2,
"loss": 4,
"goalsFor": 15,
"goalsAgainst": 14,
"points": 14
},
{
"team": "West Bromwich Albion",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 14,
"goalsAgainst": 17,
"points": 14
},
{
"team": "Crystal Palace",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 12,
"goalsAgainst": 12,
"points": 14
},
{
"team": "Watford",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 11,
"goalsAgainst": 10,
"points": 14
},
{
"team": "Stoke",
"played": 9,
"win": 4,
"draw": 1,
"loss": 4,
"goalsFor": 10,
"goalsAgainst": 9,
"points": 13
},
{
"team": "Swansea",
"played": 10,
"win": 3,
"draw": 4,
"loss": 3,
"goalsFor": 9,
"goalsAgainst": 12,
"points": 13
},
{
"team": "Everton",
"played": 11,
"win": 3,
"draw": 4,
"loss": 4,
"goalsFor": 23,
"goalsAgainst": 20,
"points": 13
},
{
"team": "Sunderland",
"played": 10,
"win": 3,
"draw": 2,
"loss": 5,
"goalsFor": 12,
"goalsAgainst": 11,
"points": 11
},
{
"team": "Bournemouth",
"played": 9,
"win": 2,
"draw": 4,
"loss": 3,
"goalsFor": 10,
"goalsAgainst": 13,
"points": 10
},
{
"team": "Newcastle United",
"played": 10,
"win": 2,
"draw": 4,
"loss": 4,
"goalsFor": 14,
"goalsAgainst": 14,
"points": 10
},
{
"team": "Aston Villa",
"played": 9,
"win": 0,
"draw": 3,
"loss": 6,
"goalsFor": 6,
"goalsAgainst": 13,
"points": 3
}
]
}
Thank You Very Much!
To create an object and add the total points for each team, you'll need to create a map (key/value) where the key is each team's name and the value is the total points for each team.
I'm hosting your data on MyJSON.
Here's a function that retrieves the data from the remote server, and initializes an object containing the total points for each team:
function getData() {
// https://api.myjson.com/bins/1c6utx
var teamScores = {};
$.get("https://api.myjson.com/bins/1c6utx", function(data, status){
$.each(data.records, function(index, value) {
if (!teamScores[value.team]) {
teamScores[value.team] = 0;
}
teamScores[value.team] += value.points;
});
console.log(JSON.stringify(teamScores));
});
}
/* Output:
{"Manchester City":24,"Arsenal":23,"Tottenham":19,"Leicester":19,"Manchester United":19,"West Ham":16,"Liverpool":15,"Norwich":15,"Southampton":14,"Chelsea":14,"West Bromwich Albion":14,"Crystal Palace":14,"Watford":14,"Stoke":13,"Swansea":13,"Everton":13,"Sunderland":11,"Bournemouth":10,"Newcastle United":10,"Aston Villa":3}
*/
You can check this fiddle to see it in action. Hope this helps!
What is the way to remove an property named "itemType"from the below given object
?
{
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"itemType": {
"id": 16,
"name": "PC 350",
"description": "PC 350"
},
"typeid": 12
}
So that the final array should look like
{
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"typeid": 12
}
This is object not array. You can use delete like this
var obj = {
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"itemType": {
"id": 16,
"name": "PC 350",
"description": "PC 350"
},
"typeid": 12
}
delete obj.itemType;
Whether it is an object and you want to delete a property from it, or you have an array and want to delete a value, the way to delete is -
delete obj.itemType //object.
delete array[3] //delete 4th item from the array.
Note - the structure you have provided is not an array, its an object.