Hello all I have a question.
With this code:
targetingIdeaService.get({selector: selector}, function (error, result) {
var string = JSON.stringify(result)
console.log(string)
})
I get this result:
"totalNumEntries":700,
"entries":[
{
"data":[
{
"key":"KEYWORD_TEXT",
"value":{
"attributes":{
"xsi:type":"StringAttribute"
},
"Attribute.Type":"StringAttribute",
"value":"nike ddd8ea95"
}
},
{
"key":"COMPETITION",
"value":{
"attributes":{
"xsi:type":"DoubleAttribute"
},
"Attribute.Type":" DoubleAttribute",
"value":"0.8726547440705715"
}
},
{
"key":"AVERAGE_CPC",
"value":{
"attributes":{
"xsi:type":"MoneyAttribute"
},
"Attribute.Type":"MoneyAttribute",
"value":{
"ComparableValue.Type":"Money",
"microAmount":"16769286"
}
}
},
{
"key":"SEARCH_VOLUME",
"value":{
"attributes":{
"x si:type":"LongAttribute"
},
"Attribute.Type":"LongAttribute",
"value":"5609289"
}
}
]
}
]
}
And with this one i get the following:
targetingIdeaService.get({selector: selector}, function (error, result) {
var resultaten = result;
var res = resultaten.entries;
for(var i = 0; i < res.length; i++){
console.log(resultaten.entries[i])
}
})
Output
{ data:
[ { key: 'KEYWORD_TEXT', value: [Object] },
{ key: 'COMPETITION', value: [Object] },
{ key: 'AVERAGE_CPC', value: [Object] },
{ key: 'SEARCH_VOLUME', value: [Object] } ] }
Now im looking to format the JSON a certain way, It has to look like this example.
Notice: the key value pairs of json data.
[
{
"KEYWORD_TEXT": "red herring 9e23f4ad",
"SEARCH_VOLUME": 4574730
},
{
"KEYWORD_TEXT": "nike 656e95f0",
"SEARCH_VOLUME": 3442386
},
etc...
]
Basically the Key and the value of that key next to eachother. How to do this?
You can map the keys to values like following -
resultaten = result.data.map(function (item) {
return {
[item.key]: item.value
}
});
JSON.stringify(resultaten);
Related
I want to pull with javascript: {"subNav0", "subNav1", "subNav2", "subNav3", "subNav4", "subNav5"}.
my json:
var data = {
"menus":{
"GrandparentNav0":{
"name":"TopNav",
"items":[
{
"name":"ParentNav0",
"iconClass":"",
"items":[
{
"name":"ParentNav1",
"iconClass":"",
"items":[
{
"name":"subNav0",
"iconClass":""
},
{
"name":"subNav1",
"iconClass":""
},
{
"name":"subNav2",
"iconClass":""
},
{
"name":"subNav3",
"iconClass":""
},
{
"name":"subNav4",
"iconClass":""
},
{
"name":"subNav5",
"iconClass":""
}
]
},
]
}
]
}
},
};
i know basic filter of an array:
data .forEach(function(o) {
o.variable = o.variable.filter(s => s.value == value);
});
I dont know how to get through menus, GrandparentNav0 to pull the subNav(s)
By "pull the subNav(s)" do you mean like accessing it through something like bracket notation?
let subNavs = data['menus']['GrandparentNav0']['items'][0]['items']
console.log(subNavs)
/* would return
[
{
"name": "subNav0",
"iconClass": ""
},
{
"name": "subNav1",
"iconClass": ""
},
{
"name": "subNav2",
"iconClass": ""
},
{
"name": "subNav3",
"iconClass": ""
},
{
"name": "subNav4",
"iconClass": ""
},
{
"name": "subNav5",
"iconClass": ""
}
]
*/
Here is a solution using object-scan. This might be overkill for your requirements, however as you run into other use cases it's a Swiss army knife that makes these types of data interactions very clean
// const objectScan = require('object-scan');
const data = { menus: { GrandparentNav0: { name: 'TopNav', items: [ { name: 'ParentNav0', iconClass: '', items: [ { name: 'ParentNav1', iconClass: '', items: [ { name: 'subNav0', iconClass: '' }, { name: 'subNav1', iconClass: '' }, { name: 'subNav2', iconClass: '' }, { name: 'subNav3', iconClass: '' }, { name: 'subNav4', iconClass: '' }, { name: 'subNav5', iconClass: '' } ] } ] } ] } } };
const result = objectScan(['menus.GrandparentNav0.items[0].items[0].items[*].name'], { reverse: false, rtn: 'value' })(data);
console.log(result);
// => [ 'subNav0', 'subNav1', 'subNav2', 'subNav3', 'subNav4', 'subNav5' ]
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#14.0.0"></script>
Disclaimer: I'm the author of object-scan
I have the below json
[
{
"fullName":"Mariem",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"endHour":"1330",
"motif":"maladie"
},
{
"fullName":"Mariem",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"endHour":"1800",
"motif":"renuion"
},
{
"fullName":"Mariem",
"startDate":"1917-05-25",
"endDate":"1917-05-25",
"endHour":"1600",
"motif":"renuion"
},
{
"fullName":"Jack",
"startDate":"0017-01-25",
"endDate":"0017-01-25",
"endHour":"1030",
"motif":null
}
]
Who can i map it like the below json, grouping the objects by fullName, startDate and endDate, and add an array of objects contients the endDate and motif.
[
{
"fullName":"Mariem ",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"data":[
{
"endHour":"1330",
"motif":"maladie"
},
{
"endHour":"1800",
"motif":"renuion"
}
]
},
{
"fullName":"Mariem ",
"startDate":"1917-05-25",
"endHour":"1917-05-25",
"data":[
{
"endHour":"1600",
"motif":"renuion"
}
]
},
{
"fullName":"Jack",
"startDate":"0017-01-25",
"endDate":"0017-01-25",
"data":[
{
"endHour":"1030",
"motif":null
}
]
}
]
If I understood your question correctly you are looking for something like this:
interface SomeObj {
fullName: string,
startDate: string,
endDate: string,
endHour: string,
motif: string
}
interface Time {
endHour:string,
motif:string
}
interface MappedObj {
fullName: string,
startDate: string,
endHour: string,
data: Time[]
}
function mapToFormat(o: SomeObj[]): MappedObj[] {
return o.map(item => {
return {
fullName: item.fullName,
startDate: item.startDate,
endHour: item.endHour,
data: [
{
endHour: item.endHour,
motif: item.motif
}
]
}
})
}
You have errors in both your sample input and output(swapping endHour and endDate).
Here is a quick solution:
const map = new Map();
elements.forEach(item => {
const key = `${item.fullName}+${item.startDate}+${item.endDate}`;
if (!map.has(key)) {
map.set(key, {
fullName: item.fullName,
startDate: item.startDate,
endDate: item.endDate,
data: []
});
}
map.get(key).data.push({
endHour: item.endHour,
motif: item.motif
});
});
const result = Array.from(map.values());
With input of:
const elements = [
{
"fullName":"Mariem",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"endHour":"1330",
"motif":"maladie"
},
{
"fullName":"Mariem",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"endHour":"1800",
"motif":"renuion"
},
{
"fullName":"Mariem",
"startDate":"1917-05-25",
"endDate":"1917-05-25",
"endHour":"1600",
"motif":"renuion"
},
{
"fullName":"Jack",
"startDate":"0017-01-25",
"endDate":"0017-01-25",
"endHour":"1030",
"motif":null
}
];
The result variable will have the value of:
[
{
"fullName":"Mariem",
"startDate":"1917-04-25",
"endDate":"1917-04-26",
"data":[
{
"endHour":"1330",
"motif":"maladie"
},
{
"endHour":"1800",
"motif":"renuion"
}
]
},
{
"fullName":"Mariem",
"startDate":"1917-05-25",
"endDate":"1917-05-25",
"data":[
{
"endHour":"1600",
"motif":"renuion"
}
]
},
{
"fullName":"Jack",
"startDate":"0017-01-25",
"endDate":"0017-01-25",
"data":[
{
"endHour":"1030",
"motif":null
}
]
}
]
My data is currently stored in this format:
{
"Site1":{
"week":[
{
"access":1
},
{
"access":8
}
]
},
"Site2":{
"week":[
{
"access":16
}
]
},
"Site3":{
"week":[
{
"access":2
},
{
"access":6
},
{
"access":2
}
]
}
}
And I need to convert it into this format:
[
{
"id":"Site1",
"access":[1,8]
},
{
"id":"Site2",
"access":[16]
},
{
"id":"Site3",
"access":[2,6,2]
}
]
As you can see, I also need to take the keys (site name) and make them the "id" values.
Any ideas on how I can do this in JavaScript (I'm using angular v9)? I'm not very good at restructuring that type of data.
You can first take entries and then map it:
var data={ "Site1":{ "week":[ { "access":1 }, { "access":8 } ] }, "Site2":{ "week":[ { "access":16 } ] }, "Site3":{ "week":[ { "access":2 }, { "access":6 }, { "access":2 } ] }};
var result = Object.entries(data).map(([k,v])=>({id:k, access: v.week.map(p=>p.access)}));
console.log(result);
Object.keys()
map()
const data = {
Site1: {
week: [
{
access: 1,
},
{
access: 8,
},
],
},
Site2: {
week: [
{
access: 16,
},
],
},
Site3: {
week: [
{
access: 2,
},
{
access: 6,
},
{
access: 2,
},
],
},
};
const result = Object.keys(data).map(key => ({
id: key,
access: data[key].week.map(w => w.access),
}));
console.log(result);
you can simply use this code for your desired result.
Object.keys(data).map(key => (
{
id: key,
access: data[key].week.map(obj => obj.access),
}
))
Let me know if you face any issue.
I am consuming XML in express server parsing it with express-xml-bodyparser, but the resulting object is basically unusable.
XML
<SubClass code="A07.0"/>
<SubClass code="A07.1"/>
<SubClass code="A07.2"/>
<SubClass code="A07.3"/>
<SubClass code="A07.8"/>
<SubClass code="A07.9"/>
is serialized as JSON
subclass:
[ { '$': { code: 'A07.0' } },
{ '$': { code: 'A07.1' } },
{ '$': { code: 'A07.2' } },
{ '$': { code: 'A07.3' } },
{ '$': { code: 'A07.8' } },
{ '$': { code: 'A07.9' } } ]
Is there way to pase it directly into
subclass: ['A07.0','A07.1','A07.2','A07.3','A07.8','A07.9']
or some easy way how to convert it into this array?
You can set mergeAttrs option to true in order to remove $ properties:
xmlparser({ mergeAttrs: true, explicitArray: false})
Output:
SubClass: [
{ code: "A07.0" },
{ code: "A07.1" },
{ code: "A07.2" },
{ code: "A07.3" },
{ code: "A07.8" },
{ code: "A07.9" }
]
Or you can just use array.map() method:
var data = { subclass:
[ { '$': { code: 'A07.0' } },
{ '$': { code: 'A07.1' } },
{ '$': { code: 'A07.2' } },
{ '$': { code: 'A07.3' } },
{ '$': { code: 'A07.8' } },
{ '$': { code: 'A07.9' } } ] };
var result = data.subclass.map( (obj) => {
return obj.$.code;
});
console.log(result);
If you want to convert json into an object then maybe something like...
var arr = Object.keys(json).map(function(x) { return obj[x] });
With jQuery
var arr = $.map(json, function(x) { return x});
I am trying to pull an array from a different collection using collection2. I have been able to do this with objects using the following example for users:
users: {
type: String,
label: "Inspector",
optional: true,
autoform: {
firstOption: 'Choose an Inspector',
options: function() {
return Meteor.users.find({}, {
sort: {
profile: 1,
firstName: 1
}
}).map(function(c) {
return {
label: c.profile.firstName + " " + c.profile.lastName,
value: c._id
};
});
}
}
},
I would like to do the same but for an array of objects. Here is what the source data looks like:
{
"_id": "xDkso4FXHt63K7evG",
"AboveGroundSections": [{
"sectionName": "one"
}, {
"sectionName": "two"
}],
"AboveGroundItems": [{
"itemSection": "one",
"itemDescription": "dfgsdfg",
"itemCode": "dsfgsdg"
}, {
"itemSection": "two",
"itemDescription": "sdfgsdfg",
"itemCode": "sdfgsdgfsd"
}]
}
Here is what my function looks like:
agSection: {
type: String,
optional: true,
autoform: {
firstOption: 'Select A Section Type',
options: function() {
return TemplateData.find({}, {
sort: {
AboveGroundSections: 1,
sectionName: [0]
}
}).map(function(c) {
return {
label: c.AboveGroundSections.sectionName,
value: c.AboveGroundSections.sectionName
}
});
}
}
},
I know this, it's just not pulling the data for me. I am sure, I am just missing something small. I am trying to pull all objects within the AboveGroundSection array.
Your .map() is iterating over the set of documents but not over the arrays inside each document. Also I don't think your sorting is going to work the way you hope because of the inner nesting.
Try:
agSection: {
type: String,
optional: true,
autoform: {
firstOption: 'Select A Section Type',
options() {
let opt = [];
TemplateData.find().forEach(c => {
c.AboveGroundSections.forEach(s => { opt.push(s.sectionName) });
});
return opt.sort().map(o => { return { label: o, value: o } });
}
}
},
Also if your AboveGroundSections array only has a single key per element then you can simplify:
"AboveGroundSections": [
{ "sectionName": "one" },
{ "sectionName": "two" }
]
To:
"AboveGroundSections": [
"one",
"two"
]