I have an object structure as given below. I had grouped like this using underscore js based on the id values
{
"1": [
{
"id": 1,
"tailName": "ABQ-PHX",
"itemId": 1,
"name": "a1",
"start": "24-11-2013,2:38",
"end": "29-11-2013,18:22"
},
{
"id": 1,
"tailName": "ABQ-PHX",
"itemId": 9,
"name": "a2",
"start": "16-12-2013,10:46",
"end": "18-12-2013,10:46"
}
],
"2": [
{
"id": 2,
"tailName": "BNA-RDU",
"itemId": 14,
"name": "b1",
"start": "21-11-2013,11:38",
"end": "15-12-2013,7:52"
}
]
}
Please advise how to access the array from the above object.
I want to access the first array (i.e)
[
{
"id": 1,
"tailName": "ABQ-PHX",
"itemId": 1,
"name": "a1",
"start": "24-11-2013,2:38",
"end": "29-11-2013,18:22"
},
{
"id": 1,
"tailName": "ABQ-PHX",
"itemId": 9,
"name": "a2",
"start": "16-12-2013,10:46",
"end": "18-12-2013,10:46"
}
]
Both your_object[1] and your_object["1"] will give you the result.
Related
I have an array of data like this one
[
{
"id": "root_01",
"parents": [
{
"id": "parent_1",
"childrens": [
{
"id": "child_01",
"name": "ABC",
"group": "group_a"
},
{
"id": "child_02",
"name": "BBC",
"group": "group_b"
}
]
},
{
"id": "parent_2",
"childrens": [
{
"id": "child_03",
"name": "CCD",
"group": "group_a"
},
{
"id": "child_04",
"name": "EEF",
"group": "group_c"
}
]
}
]
},
{} // same as previous
]
and I'm trying to get rid of all parents data & catch & merge only child items like this one:
[
{
"id": "child_01",
"name": "ABC",
"group": "group_a"
},
{
"id": "child_02",
"name": "BBC",
"group": "group_b"
},
{
"id": "child_03",
"name": "CCD",
"group": "group_a"
},
{
"id": "child_04",
"name": "EEF",
"group": "group_c"
}
]
but after reading normalizr documentation I'm a little bit confused because I couldn't find this kind of example, so can anyone suggest me is it possible to make with normalizr or any better idea?
Thanks
Recusrion is what you need here:
const arr = [ { "id": "root_01", "parents": [ { "id": "parent_1", "childrens": [ { "id": "child_01", "name": "ABC", "group": "group_a" }, { "id": "child_02", "name": "BBC", "group": "group_b" } ] }, { "id": "parent_2", "childrens": [ { "id": "child_03", "name": "CCD", "group": "group_a" }, { "id": "child_04", "name": "EEF", "group": "group_c" } ] } ] }];
const flatten = arr => arr.flatMap(o=>o.parents ? flatten(o.parents) : o.childrens ? flatten(o.childrens) : o);
console.log(flatten(arr));
I have json data like this:
[{
"Id": 1,
"Name": "Test 1",
"Time": {
"start": "22:00",
"end": "22:30",
"duration": 30
}
}, {
"Id": 2,
"Name": "Test 2",
"Time": {
"start": "22:05",
"end": "22:35",
"duration": 30
}
}, {
"Id": 3,
"Name": "Test 3",
"Time": {
"start": "23:25",
"end": "23:40",
"duration": 15
}
},{
"Id": 4,
"Name": "Test 4",
"Time": {
"start": "22:15",
"end": "22:50",
"duration": 15
}
}
]
And i want get result like this, determine which intersect by Time:
[
["Test 1", "Test 2", "Test 4"],
["Test 3"]
]
I have solution, but it's too complicated and i use 4 loops inside each other
If there is an easier solution, please suggest, thanks!
Try aggregation, I am not sure this will work exact as per your requirement,
$addFields add new field startHour, that splits string hour and minute from string, using $split, and $arrayElemAt return first element means hour
$group by startHour and make array of Name
db.collection.aggregate([
{
$addFields: {
startHour: {
$arrayElemAt: [{ $split: ["$Time.start", ":"] }, 0]
}
}
},
{
$group: {
_id: "$startHour",
Name: { $push: "$Name" }
}
}
])
Playground
Result:
[
{
"Name": ["Test 1","Test 2","Test 4"],
"_id": "22"
},
{
"Name": ["Test 3"],
"_id": "23"
}
]
I would like to convert HTML site to Javascript Object, For example if user given the url in text box "http://Host/page" then need to scrape the website and convert it to javascript object like below ,
{
"rootId": 1,
"children": [
{
"nodeType": 10,
"id": 2,
"name": "html",
"publicId": "",
"systemId": ""
},
{
"nodeType": 1,
"id": 3,
"tagName": "HTML",
"attributes": {
"lang": "en"
},
"childNodes": [
{
"nodeType": 1,
"id": 4,
"tagName": "HEAD",
"attributes": {},
"childNodes": [
{
"nodeType": 3,
"id": 5,
"textContent": "\n"
},
{
"nodeType": 1,
"id": 6,
"tagName": "TITLE",
"attributes": {},
"childNodes": [
{
"nodeType": 3,
"id": 7,
"textContent": "About"
}
]
},......
Any Help Appreciated
Try yahoo query language (YQL)
https://developer.yahoo.com/yql/guide/select.html
Please help me with this customization of objects in javascript, I'm a little stuck.
Object
var stocks = [
{
"size": "2",
"count": 5,
"store": {
"id": 3,
"title": "Belconnen"
}
},
{
"size": "3",
"count": 4,
"store": {
"id": 4,
"title": "Canberra"
}
},
{
"size": "4",
"count": 4,
"store": {
"id": 5,
"title": "Bankstown"
}
},
{
"size": "5",
"count": 5,
"store": {
"id": 4,
"title": "Canberra"
}
}];
Expected Output
{3:{"sizes": {2: 3}}},
{4:{"sizes": {3: 3, 5: 3}}},
{5:{"sizes": {4: 3}}}
My code so far
for (var key in stocks) {
var stock = stocks[key];
stores[stock.store.id] = stock.store;
var store = stores[stock.store.id];
if (!store.sizes || !store.sizes.length) {
store.sizes = [stock.size];
}
}
But it keeps overiding the first size value of the store.
Thanks in advance.
This is one way to solve the problem
var stocks = [
{"size": 2, "count": 3, "store": {"id": 3, "name": "Canberra"}},
{"size": 3, "count": 3, "store": {"id": 4, "name": "Belconnen"}},
{"size": 4, "count": 3, "store": {"id": 5, "name": "Bankstown"}},
{"size": 5, "count": 3, "store": {"id": 4, "name": "Belconnen"}}];
var obj = {};
stocks.forEach(function(item){
// initialize if object key not present
if(!obj[item.store.id]){
obj[item.store.id] = {"sizes": {}};
}
obj[item.store.id]["sizes"][item.size] = item.count;
});
console.log(obj);
Here is the working fiddle https://jsfiddle.net/flyinggambit/nc3uny53/
Check the console window for fiddle output
How do I configure the no-multi-spaces rule to allow the following:
var arr = [
{id: 'abc', content: 'foo'},
{id: 'cdefgh', content: 'bar'}
];
By default it complains about the space before content. I guess I need to add an AST node to the exceptions but I don't know which.
In the documentation it is stated:
The easiest way to determine the node types for exceptions is to use the online demo.
So I went ahead and put your code in there and got the AST. The following part seems to be the relevant one:
{
"type": "ObjectExpression",
"start": 16,
"end": 46,
"properties": [
{
"type": "Property",
"start": 17,
"end": 26,
"key": {
"type": "Identifier",
"start": 17,
"end": 19,
"name": "id"
},
"value": {
"type": "Literal",
"start": 21,
"end": 26,
"value": "abc",
"raw": "'abc'"
},
"kind": "init"
},
{
"type": "Property",
"start": 31,
"end": 45,
"key": {
"type": "Identifier",
"start": 31,
"end": 38,
"name": "content"
},
"value": {
"type": "Literal",
"start": 40,
"end": 45,
"value": "foo",
"raw": "'foo'"
},
"kind": "init"
}
]
},
Since your code seems to pertain to the whole object, I would guess that the AST node you seek is ObjectExpression.
/* eslint no-multi-spaces: [2, { exceptions: { "ObjectExpression": true } }] */
Please let me know if this works.