I'm trying to organise data from an api that returns the following json:
[
{
"0": "THURSDAY 25th MARCH",
"1": "",
"2": ""
},
{
"0": "Snow Patrol",
"1": "Press Conference",
"2": "16:00 - 19:35"
},
{
"0": "",
"1": "",
"2": ""
},
{
"0": "FRIDAY 26th MARCH",
"1": "",
"2": ""
},
{
"0": "Arctic Moneys",
"1": "Concert",
"2": "11:55 - 12:40"
},
{
"0": "Rush",
"1": "Practice Session",
"2": "13:05 - 13:50"
}
]
The api returns an array with only numbers as keys and the next date starts after and empty object.
Into an organised array like so:
[
{
"date": "THURSDAY 25th MARCH",
"events": [
{
"band": "Snow Patrol",
"type": "Press Conference",
"time": "16:00 - 19:35"
}
]
},
{
"date": "FRIDAY 26th MARCH",
"events": [
{
"band": "Arctic Moneys",
"type": "Concert",
"time": "11:55 - 12:40"
},
{
"band": "Rush",
"type": "Practice Session",
"time": "13:05 - 13:50"
}
]
}
]
Any help would be much appreciated!
Cheers!
You could create an array of each object and check if all vlaues are empty strings or the last two ones.
The continue or build a new date object and add later all events to the last object.
const
data = [{ 0: "THURSDAY 25th MARCH", 1: "", 2: "" }, { 0: "Snow Patrol", 1: "Press Conference", 2: "16:00 - 19:35" }, { 0: "", 1: "", 2: "" }, { 0: "FRIDAY 26th MARCH", 1: "", 2: "" }, { 0: "Arctic Moneys", 1: "Concert", 2: "11:55 - 12:40" }, { 0: "Rush", 1: "Practice Session", 2: "13:05 - 13:50" }],
result = data.reduce((r, o) => {
const a = Object.assign([], o);
if (!a.join('')) return r;
if (!a.slice(1).join('')) {
r.push({ date: a[0], events: [] });
} else {
const [band, type, time] = a;
r[r.length - 1].events.push({ band, type, time });
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
i have a array which looks like this :
[
{
"1": {
"id": 1,
"price": 569.2,
"marketcap": 94943213384.68,
"timestamp": 1637136047,
"datetime": "2021-11-17 08:00:47"
},
"2": {
"id": 2,
"price": 59745.7,
"marketcap": 1127634159487.02,
"timestamp": 1637136064,
"datetime": "2021-11-17 08:01:04"
},
"3": {
"id": 3,
"price": 592.53,
"marketcap": 11199494644.95126,
"timestamp": 1637136053,
"datetime": "2021-11-17 08:00:53"
}
},
{
"1": {
"id": 1,
"last30dcommits": null,
"activity": "No Github Repo Found"
},
"2": {
"id": 2,
"last30dcommits": 109,
"activity": "High Activity"
},
"3": {
"id": 3,
"last30dcommits": 44,
"activity": "Medium Activity"
}
},
{
"1": {
"id": 1,
"change24h": -6.75,
"change7d": -10.18,
"change30d": 17.32,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"2": {
"id": 2,
"change24h": -3.22,
"change7d": -12.58,
"change30d": -5.17,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"3": {
"id": 3,
"change24h": -6.37,
"change7d": -17.81,
"change30d": -5.04,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
}
},
{
"1": {
"tweetsRating": 44.04,
"newsRating": 38.42
},
"2": {
"tweetsRating": 14.76,
"newsRating": -1.27
},
"3": {
"tweetsRating": 0,
"newsRating": 44.35
}
},
]
I am implementing a pagination in the front end in which I am slicing each objects in each object in the array to a given value so that I get a result something like this if I slice (0,2)
[
{
"1": {
"id": 1,
"price": 569.2,
"marketcap": 94943213384.68,
"timestamp": 1637136047,
"datetime": "2021-11-17 08:00:47"
},
"2": {
"id": 2,
"price": 59745.7,
"marketcap": 1127634159487.02,
"timestamp": 1637136064,
"datetime": "2021-11-17 08:01:04"
}
},
{
"1": {
"id": 1,
"last30dcommits": null,
"activity": "No Github Repo Found"
},
"2": {
"id": 2,
"last30dcommits": 109,
"activity": "High Activity"
}
},
{
"1": {
"id": 1,
"change24h": -6.75,
"change7d": -10.18,
"change30d": 17.32,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"2": {
"id": 2,
"change24h": -3.22,
"change7d": -12.58,
"change30d": -5.17,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
}
},
{
"1": {
"tweetsRating": 44.04,
"newsRating": 38.42
},
"2": {
"tweetsRating": 14.76,
"newsRating": -1.27
}
},
]
means only 2 objects should be sliced out.
my function is as following but its not working.
const sliceData = (array,start,end)=>{
array.forEach(arr=>{
Object.keys(arr).slice(start - 1, end + 1).reduce((result, key) => {
result[key] = arr[key];
return result;
}
})
}
Here start and end represents the index of starting object in a page and index of last object in the page respectively.
Can anyone help me with the code? I'll be very grateful.
You could slice the entries for new objects.
const
data = [{ "1": { id: 1, price: 569.2, marketcap: 94943213384.68, timestamp: 1637136047, datetime: "2021-11-17 08:00:47" }, "2": { id: 2, price: 59745.7, marketcap: 1127634159487.02, timestamp: 1637136064, datetime: "2021-11-17 08:01:04" }, "3": { id: 3, price: 592.53, marketcap: 11199494644.95126, timestamp: 1637136053, datetime: "2021-11-17 08:00:53" } }, { "1": { id: 1, last30dcommits: null, activity: "No Github Repo Found" }, "2": { id: 2, last30dcommits: 109, activity: "High Activity" }, "3": { id: 3, last30dcommits: 44, activity: "Medium Activity" } }, { "1": { id: 1, change24h: -6.75, change7d: -10.18, change30d: 17.32, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "2": { id: 2, change24h: -3.22, change7d: -12.58, change30d: -5.17, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "3": { id: 3, change24h: -6.37, change7d: -17.81, change30d: -5.04, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 } }, { "1": { tweetsRating: 44.04, newsRating: 38.42 }, "2": { tweetsRating: 14.76, newsRating: -1.27 }, "3": { tweetsRating: 0, newsRating: 44.35 } }],
result = data.map(o => Object.fromEntries(Object.entries(o).slice(0, 2)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I really think the data format/structure sent from BackEnd should be changed. Instead of sending Array of objects you should consider sending Array of Array of objects from Backend. This is because essentially the id of the object is key. However, Here is how you can slice the data if you do not have permission to change data sent from Backend.
const sliceData = (array,start,end)=>{
return array.map(el => {
let newObj = Object.values(el).filter(x => x.id>start && x.id<=end).reduce((acc,x) => {
acc[x.id] = x;
return acc;
} ,{})
return newObj;
});
}
I have this object value
var data = {
"questions": {
"0": {
"0": "17",
"1": "12"
},
"1": {
"0": "22",
"1": "34"
},
"2": {
"0": "52",
"1": "61"
}
}
}
I am trying to get value from these objects as I have tried below things which return me other than what I actually want.
alert(Object.keys(data.questions[0])); // Output : 0,1
alert(Object.keys(data.questions[0][0])); // Output : 0
alert(Object.keys(data.questions[0][1])); // Output : 0
Anyone can help me find the value of above keys like:
questions[0][0] = 17
questions[0][1] = 12
Try like this.
var data = {
"questions": {
"0": {
"0": "17",
"1": "12"
},
"1": {
"0": "22",
"1": "34"
},
"2": {
"0": "52",
"1": "61"
}
}
}
console.log(data.questions["0"]);
console.log(data.questions["0"]["0"]);
console.log(data.questions["0"]["1"]);
To get length of any particular question (in your data structure) use
Object.keys(data.questions["0"]) or Object.keys(data.questions["1"])
to get value of any questions use
data.questions["0"]["0"] or data.questions["0"]["1"] or data.questions["1"]["0"] and so on..
You get the result without Object.keys.
var data = { questions: { 0: { 0: "17", 1: "12" }, 1: { 0: "22", 1: "34" }, 2: { 0: "52", 1: "61" } } };
console.log(data.questions[0]); // { 0: "17", 1: "12" }
console.log(data.questions[0][0]); // 17
console.log(data.questions[0][1]); // 12
For searching a value's path of keys, you could use an iterative and recursive approach by checking all keys and objects.
function findValue(object, value) {
var p;
Object.keys(object).some(function (k) {
var t;
if (object[k] === value) {
p = [k];
return true;
}
if (object[k] && typeof object[k] === 'object' && (t = findValue(object[k], value))) {
p = [k].concat(t);
return true;
}
});
return p;
}
var data = { questions: { 0: { 0: "17", 1: "12" }, 1: { 0: "22", 1: "34" }, 2: { 0: "52", 1: "61" } } };
console.log(findValue(data, '17'));
console.log(findValue(data, '34'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Can you change the data structure? If I were you I would change it to the following:
var data = {
"questions": [
[17, 22],
[22, 34],
[52, 61]
]
};
console.log(data.questions[0]); // Output : 17,22
console.log(data.questions[0][0]); // Output : 17
console.log(data.questions[1][1]); // Output : 34
To access the first object ["0"]
To access the first two object : ["0"]["0"] and so on
based on the above expression we can access the objects like this
var data = {
"questions": {
"0": {
"0": "17",
"1": "12"
},
"1": {
"0": "22",
"1": "34"
},
"2": {
"0": "52",
"1": "61"
}
}
}
console.log(data.questions["0"]);
console.log(data.questions["0"]["0"]);
console.log(data.questions["0"]["1"]);
console.log(data.questions["1"]["1"]);
I have the following Javascript object:
var data = {
"customerSiteName": "Caldwell Sugar",
"AgreementID": "0",
"AgreementType": "",
"AgreementTypeID": {
"1": "Percentage Support",
"2": "Consignment Support",
"7": "Mobile Solutions",
"9": "SmartGlance Subscription",
"10": "Customer FIRST Lite",
"11": "Solution Support - LPS",
"12": "InSight Subscription"
},
"ProgramLevel": "",
"CFProgramLevelID": [
[1, "Primary"],
[2, "Standard"],
[3, "Premium"],
[4, "Elite"]
],
"DistributorID": "16",
"StartDate": "",
"EndDate": ""
};
I need to convert the key CFProgramLevelID which is an Array into a one dimension object. This is what I have tried so far:
$.each(data, function(key, value) {
if (value !== null && value instanceof Array) {
var obj = dataObj = value.reduce((p, c, i) => (Array.isArray(c) && (p[i] ={[c.length - 1]: c[c.length - 1]}), p), {});
console.log(obj);
}
});
But each value in CFProgramLevelID is converted to an object returning in this:
Object {0: Object, 1: Object, 2: Object, 3: Object}
0: Object
1: "Primary"
__proto__: Object
1: Object
1: "Standard"
__proto__: Object
2: Object
1: "Premium"
__proto__: Object
3: Object
1: "Elite"
__proto__: Object
What I want to get is as follow:
"CFProgramLevelID": {
"1": "Primary",
"2": "Standard",
"3": "Premium",
"4": "Elite"
}
What I am doing wrong?
I forgot to mention I have created a jsFiddle here
Updated your code, please see below (be mindful that this each loop will affect any array values that you have for a JSON key pair value, not just for CFProgramLevelID):
var data = {
"customerSiteName": "Caldwell Sugar",
"AgreementID": "0",
"AgreementType": "",
"AgreementTypeID": {
"1": "Percentage Support",
"2": "Consignment Support",
"7": "Mobile Solutions",
"9": "SmartGlance Subscription",
"10": "Customer FIRST Lite",
"11": "Solution Support - LPS",
"12": "InSight Subscription"
},
"ProgramLevel": "",
"CFProgramLevelID": [
[1, "Primary"],
[2, "Standard"],
[3, "Premium"],
[4, "Elite"]
],
"DistributorID": "16",
"StartDate": "",
"EndDate": ""
};
$.each(data, function(key, value) {
if (value !== null && value instanceof Array) {
var obj = dataObj = value.reduce((p, c, i) => (Array.isArray(c) && (p[i + 1] = c[c.length - 1]), p), {});
data[key] = obj;
console.log(obj);
console.log(data);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Your may do as follows;
var data = {
"customerSiteName": "Caldwell Sugar",
"AgreementID": "0",
"AgreementType": "",
"AgreementTypeID": {
"1": "Percentage Support",
"2": "Consignment Support",
"7": "Mobile Solutions",
"9": "SmartGlance Subscription",
"10": "Customer FIRST Lite",
"11": "Solution Support - LPS",
"12": "InSight Subscription"
},
"ProgramLevel": "",
"CFProgramLevelID": [
[1, "Primary"],
[2, "Standard"],
[3, "Premium"],
[4, "Elite"]
],
"DistributorID": "16",
"StartDate": "",
"EndDate": ""
},
result = data.CFProgramLevelID.reduce((r,sa) => Object.assign(r,{[sa[0]]:sa[1]}), {});
console.log(result);
Please try the following code.
var objectConstructor = {}.constructor;
var data = {}
object = {
"billing_address": {
"billingAddress": "d",
"billingCity": "e",
"billingState": "f"
},
"shipping_address": {
"shippingAddress": "a",
"shippingCity": "b",
"shippingState": "c"
}
}
a(object);
function a(obj, key) {
if (!obj) {
return;
} else if (obj.constructor !== objectConstructor) {
data[key] = obj;
} else {
Object.keys(obj).map((key, index) => {
a(obj[key], key);
})
}
}
console.log(data)
I hope it helps.
Thanks.
I have an object that looks like this:
var ingredientsObject = {
"Ingredients": [
{ "Section": "Ingredienser", "Name": "salt", "Value": 1, "Unit": "tsk" },
{ "Section": "Ingredienser", "Name": "olivolja", "Value": 1, "Unit": "msk" },
{ "Section": "Ingredienser", "Name": "lasagneplattor, (125 g) färska", "Value": 6, "Unit": "st" },
{ "Section": "Tomatsås", "Name": "salt", "Value": 0.5, "Unit": "tsk" },
{ "Section": "Tomatsås", "Name": "strösocker", "Value": 2, "Unit": "krm" }
{ "Section": "Béchamelsås", "Name": "salt", "Value": 0.5, "Unit": "tsk" },
{ "Section": "Béchamelsås", "Name": "smör", "Value": 2.5, "Unit": "msk" }
]
};
and I am trying to recalculate the value of each ingredient based on the number of servings specified using underscore.
I have tried using mapObject (http://underscorejs.org/#mapObject):
newIngredients = _.mapObject(ingredients.Ingredients, function (val, key) {
return val.Value / modifier;
});
but that returns an object that looks like this:
Object {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 2, 3: 0.3333333333333333, 4: 50, 5: 66.66666666666667, 6: 0.16666666666666666, 7: 0.6666666666666666, 8: 0.25, 9: 0.3333333333333333, 10: 0.3333333333333333, 11: 0.16666666666666666, 12: 0.8333333333333334, 13: 0.16666666666666666, 14: 0.8333333333333334, 15: 1.6666666666666667, 16: 0.6666666666666666}
whereas what I actually want is the original object with only the values changed, as in:
var ingredientsObject = {
"Ingredients": [
{ "Section": "Ingredienser", "Name": "salt", "Value": 0.3333333333333333, "Unit": "tsk" },
{ "Section": "Ingredienser", "Name": "olivolja", "Value": 0.3333333333333333, "Unit": "msk" },
{ "Section": "Ingredienser", "Name": "lasagneplattor, (125 g) färska", "Value": 2, "Unit": "st" }
// and so on...
]
};
How do I achieve this?
Actually ingredients.Ingredients is an array, _.mapObejct expect a object as the first param. You can do this in underscore way:
_.mapObject(ingredientsObject, function(val, key) {
return _.map(val, function(ingredient) {
return _.extend(
{},
ingredient,
{
Value: ingredient.Value / modifier
}
)
})
})
Ok, based on the comments and suggestions I received, I came up with this solution:
newIngredients = _.each(ingredientsObject, function (list) {
_.each(list, function (item) {
item.Value = item.Value / modifier;
});
});
This modifies the value itself without modifying the object structure.
Thanks #nnnnnn for pointing me in the right direction.
Try:
newIngredients = _.map(ingredientsObject.Ingredients, function(item) {
return {
Section: item.Section,
Name: item.Name,
Value: item.Value / modifier,
Unit: item.Unit
};
});
I'm having a hard time wrapping my head around key value (which are arrays) into a node based/treemap structure. Below is some sample code to help explain.
I'd like to convert this...
// key value data
var rawData = {
"1": [],
"2": [10],
"3": [2,5,11],
"4": [],
"5": [1,7,6],
"6": [4],
"7": [],
"8": [9],
"9": [],
"10": [],
"11": []
}
to this tree map...
// tree map
var treeData = {
"id": "ALL",
"contents": [
{
"id": "3",
"contents": [
{
"id": "2",
"contents": [
{ "id": "10" }
]
}, {
"id": "5",
"contents": [
{ "id": "1" },
{ "id": "7" },
{
"id": "6",
"contents": [
{ "id": "4" }
]
}
]
},
{ "id": "11" }
]
},
{
"id": "8",
"contents": [
{ "id": "9" }
]
}
]
}
I believe some form of recursion is involved but I'm having a tough time traversing through the nodes...
var traverse = function(rawData) {
for (var i in rawData){
var datum = rawData[i];
for (var j in datum) {
if(i===datum[j]){
// tack on node to datum
}
traverse(rawData);
}
}
}
Iterating through array in not possible with for in, instead:
for (var j = 0; j < datum.length; i++) {
if(i===datum[j]){
// tack on node to datum
}
traverse(rawData);
}