convert value in txt file to object json - javascript

[2023-01-28 17:54:59]Name John, Cart Iphone13
[2023-01-28 17:54:59]Name John, Cart Iphone14
[2023-01-28 17:54:59]Name John, Cart Iphone15
[2023-01-28 17:54:59]Name John, Cart Iphone16
[2023-01-28 18:24:29]Name Frank, Cart Android10
[2023-01-28 18:24:29]Name Frank, Cart Android11
I have data in .txt file like this. how convert to object JSON and groupBy same date and name in javascript? I need result like this
[
{
"date": "2023-01-28 17:54:59",
"name": "John",
"cart": [
"Iphone13",
"Iphone14",
"Iphone15",
"Iphone16"
]
},
{
"date": "2023-01-28 18:24:29",
"name": "Frank",
"cart": [
"Android10",
"Android11"
]
}
]

I think the following logic would do it good.
Load and parse each line of txt file to a list of item class.
class item - with date, name, and list of devices.
Then you can check if the name is already there, add the device into the list of devices.
Then you can stringify the class - refer this

Related

How to access "text:" property in the html document or console.log

I have the following array and want to access the value under the text property:
[
{"detectedLanguage":
{"language": "hin", "score": 1.0},
"translations":[
{"text":"I am a boy", "to":"en"}
]
}
]
How can i achieve this in console.log or html?
You can make a javscript object to get value of text (you can do this ):
let array = [
{
"detectedLanguage":{
"language":"hin",
"score":1.0
},
"translations":[
{
"text":"I am a boy",
"to":"en"
}
]
}
]
console.log(array[0].translations[0].text)
Your structure is the following:
Array[
Object{"detectedLanguage":
Object{"language": "hin", "score": 1.0},
"translations":Array[
Object{"text":"I am a boy", "to":"en"}
]
}
]
To access an array you use the index of the items. So in your example there is only one item, you could access it with Array[0]. Then you have this object:
{"detectedLanguage":
{"language": "hin", "score": 1.0},
"translations":[
{"text":"I am a boy", "to":"en"}
]
}
There are two keys in you object, called detectedLanguage. Trying to reach text you need to access the translations key like Array[0].translations would give you the following object:
[{"text":"I am a boy", "to":"en"}]
This is another array with an object as the only item. So we can directly reach to text this time by calling Array[0].translations[0].text as we refer to the first item of translations and the key text of the object, so we get:
I am a boy
To call this like this Array[0].translations[0].text we need to store Array in a actual array like:
let myArray = [
{"detectedLanguage":
{"language": "hin", "score": 1.0},
"translations":[
{"text":"I am a boy", "to":"en"}
]
}
]
And then you can just console.log(myArray[0].translations[0].text).

How to access elements of Firebase Snapshot?

I have a Firebase database that looks like this:
masterSheet
0
0: 0
1: "What is your favorite type of dog?"
2: "Some values"
3: "Some more values"
4: "Blah blah"
5: "Animals"
1
0: 1
1: "What's 2 + 2?"
I'm using this code to retrieve a snapshot of the databse.
var database = firebase.database();
var ref = database.ref('masterSheet')
ref.on("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var data = childSnapshot.val();
console.log(data);
})
})
This seems to work, as when I run it in a browser and do inspect element > console, I can see:
(6) [0, "What is your favorite type of dog?", "Some values", etc
(6) [1, "What's 2 + 2?", "Some values", etc
The problem is that I can't seem to access this data like a normal JavaScript object. I'm using this for a web app quiz, and need to be able to access each question individually and the associated values.
If I try to access the first value in the object with console.log(data[0]), this is the result:
0
1
2
3
4
etc
Which seems right, but if I do data[1], the result is "undefined."
What am I doing wrong here? Basically I need to make a list of dictionary objects, or a nested dictionary, or something where I can access each question by calling the index/number associated with it. I would like to be able to make an object where the keys are the question numbers (0-indexed) and then the rest of the values are in nested dictionaries within that object.
But for the last couple days I've been trying to figure out how to access this Firebase snapshot and just can't figure out the structure.
Edit: Here is the structure of my JSON.
{
"masterSheet" : [ [ 0, "What's 2+2?", "22", "4", "6", "Math question" ], [ 1, "What's the capital of Nevada?", "Reno", "Las Vegas", "Carson City", "Geography" ], [ 2, "What's the meaning of life?", "40", "41", "42", "Philosophy" ] ]
}

Get info from Array then Object in Javascript

I'm trying to display data after a Fetch. I grouped this data by date so I grouped my objects into an array which have the date as main key.
But now, I'm kind of lost and don't know how to display get the date as Header section then the objects.
This is my data:
"31 janvier 2015": Array [
Object {
"image": "image",
"name": "name",
},
Object {
"image": "image",
"name": "name",
},
],
"02 février 2016": Array [
Object {
"image": "image",
"name": "name",
},
Object {
"image": "image",
"name": "name",
},
]
What I would like to do is to display it like a section list :
31 janvier 2015
> object
> object
02 février 2016
> object
> object
I think I can map the objects but first I have to get the date and go inside that array.
What you have is an associative array, that is an array that instead of numeric indexes has strings. It works just like an object would if you were for example to do person['age'] on a person object.
You can loop through the "indexes" with the below code, checking hasOwnProperty to avoid any inherited properties. You can then access your dates by key
for (var key in MainArray) {
if (MainArray.hasOwnProperty(key))
console.log(MainArray[key]);
}
Iterate over the map keys, displaying the date and listing the items.
for(let date in list){
// Date as SECTION HEADING
console.log(date);
// access items...
const items = list[date];
// Display item
items.forEach(console.log);
}

What is the correct format of populating a JSON object with a nested array of objects?

I'm trying to create a JSON object with a nested array of JSON objects. What is the correct format of this?
Here is an example what I am trying to create:
{
"reviewCount": 96,
"reviews": [
{"name": "Sean Steinman", "date": "reviewed 2 weeks ago", "reviewContent": "Fantastic Service"},
{"name": "Ryan Lundell", "date": "reviewed in the last week", "reviewContent":"Ask for Scott!"}
]
}
Here is what I have so far:
var reviewObj = {
reviewCount: reviews.length,
reviews: [{name: , date: , reviewContent:}]
}
After I initialize it, I will fill it with a for loop that runs through an existing array of strings.
CLARIFICATION:
The array that I'm using to populate the JSON object is:
[
"\nSean Steinman\nreviewed 2 weeks ago\n Fantastic Service\n",
"\nRyan Lundell\nreviewed in the last week\n Ask for Scott!\n• • •\n"
]
So I'm creating a new array in my for with tmpArr = reviews[i].split('/n');, and then where I'm getting stuck is how to stick that into the JSON object as an object.
First, you're not building a "JSON" object. You're just building an object. It's not JSON until you JSON-encode it. {"name": "bob"} is not JSON, it's an object literal. '{"name": "bob"}', the string, is JSON.
Second, you cannot loop inside an object literal, which is what your second code example seems to indicate you're trying to do. Instead, you need to initialize you reviews property to an empty array, and then loop and append items to the array.
var reviews = [
"\nSean Steinman\nreviewed 2 weeks ago\n Fantastic Service\n",
"\nRyan Lundell\nreviewed in the last week\n Ask for Scott!\n• • •\n"
];
var reviewObj = {
reviewCount: reviews.length,
reviews: []
}
reviews.forEach(function(line) {
var review = line.split("\n");
reviewObj.reviews.push({name: review[0], date: review[1], reviewContent: review[2]});
});

Javascript nested arrays

I am currently developing a website to do with cooking and will store recipes.
At the moment I am planning to store the recipes in a JS nested array. The array will contain all the recipes and then within each of the recipes will be another array containing all the ingredients for that recipe.
What would be the best way to structure this nested array?
Currently I have the following but I'm not entirely sure this is the best/correct way to do it...
Any help is much appreciated.
var recipes = [
{
name:"pizza",
ingredients: {
"tomato",
"cheese",
"meat"
}
}
]
I agree with #smakateer regarding associated array. However I would improve it a bit to:
var recipes = {
"pizza": {
"ingredients": ["tomato", "cheese", "meat" ], //or "ingredients": [ {"name":"tomato", "howMany": 3} ]
//thanks to this it will be easier extendable, i.e.
"description": "Some description",
imageUrl: URL
}
}
EDIT:
You'll probably have many receips for pizza, so you could store them in array of objects under one key.
...
"pizza": [ {...}, {...} ],
"dumplings": []
...
I would suggest moving the name to the top level of the array and switching it to an associated array of strings to arrays:
var recipes = {
"pizza": [
"tomato",
"cheese",
"meat"
]
}
Then you can call each recipe by name and get the iterable list of ingredients.

Categories