Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to push data at the end of the student tags array which is in an object, I was wondering how I could do this?
var tags = "2019 Student"
{
"first_name": "Thomas",
"last_name": "Lee",
"student_tags":
["2020 Student"]
}
Use the push() to add item to an Array.
let tags = "2019 Student"
let obj = {
"first_name": "Thomas",
"last_name": "Lee",
"student_tags": ["2020 Student"]
};
obj.student_tags.push(tags);
console.log(obj);
Use push on student_tags:
obj.student_tags.push(tags);
var tags = "2019 Student"
var student = {
"first_name": "Thomas",
"last_name": "Lee",
"email": "test#email.tel",
"student_tags":
["2020 Student"]
}
You can just go to the student_tags property and push the tag.
student.student_tags.push(tags);
Related
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 last month.
Improve this question
I am sorry if this is a newbie question, I am recently started to work with Javascript.
I've got a string that looks like this:
myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming"
My desired outcome would be a json like the following:
[
{
"id": "101"
"Name": "Maria",
"LastName": "Smith"
"Job": "Teacher",
"Salary": "26000",
"FavSport": "Football"
},
{
"id": "102"
"Name": "Albert",
"LastName": "Sullivan"
"Job": "Pianist",
"Salary": "85000",
"FavSport": "Swimming"
}
]
I have tried using myString.split('#') but it returns an array that is still separated by vertical bars and that I am not able to separate. Any ideas are highly appreciated
Split by space " " to get first your groups
Then, split by pipe "|" to get an array of values
Then .map() your array to a new Array of objects
const myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming";
const groups = myString.split(" ");
const result = groups.map((groupString) => {
const [id, Name, LastName, Job, Salary, FavSport] = groupString.split("|");
return {id: id.replace("#", ""), Name, LastName, Job, Salary, FavSport};
});
console.log(result)
Then, to convert it to a JSON String use:
const json = JSON.stringify(result);
PS: Anyways, I would highly discourage you from using that input string format
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 1 year ago.
Improve this question
Hey please don't roast me. So i have a JSON data like this
data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
],
"person": [
{
"id": "xx2",
"name": "John Snow",
},
],
}
For example, I want to store every person data whose name contains "o". How could I do that? Thank you
JSON object ,if have duplicate keys, it will replace the first one with the most bottom one .
In your sample data, there is two "person" keys. Therefore, at the end, your said data will succumb to this.
{"data":{"person":[{"id":"xx2","name":"John Snow"}]}}
Therefore, to clarify your data, IMHO, this is supposed to be like this in the first place.
"data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
{
"id": "xx2",
"name": "John Snow",
},
]
}
Then you can treat data.person as an array and use Array.prototype function to filter out your desired details.
The filter method should be
const personWithNameO = data.person.filter((v) => v.name.includes("o") );
console.log(personWithNameO);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm currently working on a project with Redux and React.
To fill in an input I have to request the server and the server returns an array of times(it may have one member or several).
like this:
{
"start_hour": [
"10:00",
"15:55",
"20:08"
]
}
I have to separate the hours and minutes and have two states.
like this:
{
"hours": [
"10",
"15",
"20"
];
"minutes": [
"00",
"55",
"08"
];
}
How can I do this? Are there any libraries for this?
Thanks for the Help.
Using Array.prototype.reduce and String.split function, this can be done as follows.
const input = {
"start_hour": [
"10:00",
"15:55",
"20:08"
]
};
const output = input.start_hour.reduce((acc, cur) => {
const curArr = cur.split(':');
acc.hours.push(curArr[0]);
acc.minutes.push(curArr[1]);
return acc;
}, { hours: [], minutes: [] });
console.log(output);
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 2 years ago.
Improve this question
I have scraped a webpage using BeautifulSoup and requests and following piece of code is a portion from the whole HTML page.
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
There is an object with key "people". How can I extract the value of this key using regex i.e.
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]
so that I can parse it as a Python dictionary. I can confirm that string "people" (with quotes) is unique on the whole page.
So far, I've seen some solutions to extract strings enclosed in quotes. But in this case, I have to use a pattern that matches "people": and maybe "attributes" key after it can also be used as a stopping point, I have to extract what's in the middle of these two. Thanks in advance!
You can use re/ast modules to extract the data. For example:
import re
from ast import literal_eval
txt = r'''
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
'''
data = re.search(r'("people".*?),\s*\n', txt, flags=re.S).group(1)
data = literal_eval('{' + data + '}')
# print the result:
print(data['people'])
Prints:
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]
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 have an object like below:
Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ], parent2{...........}.... };
How can I make it nested JSON.
Just like in root: parent1, parent2...
Child: children1, ....( Corresponding to parent)
Grandchildren: based on children
Please guide me how can I make it?
To make a JSON object you just have to follow JSON syntax
In your example it will look like:
{
"parent": {
"child1": [{
"name": "grandchild1",
"value": "abc",
"checked": "true"
}, {
"name": "grandchild2",
"value": "pqr",
"checked": "false"
}]
}
}
Please note that in JSON, string values must be written with double quotes.
If you need to get JSON string from object programmatically, you can write in javascript:
var myJSON = JSON.stringify(obj);