JSON jQuery auto-fill [closed] - javascript

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 1 year ago.
Improve this question
first of all, I really haven't learn any javascript at all, and I am stuck here.
I want to get these link in this JSON example.
"list": [
[
"List of recipes",
[
{
"name": "Eggs",
"links": [
{
"method": "Fry",
"link": "https://example.com/category/fry"
},
]
},
]
]
I can't elaborate with my friend's example code, can anyone help me?

Your JSON isn't valid so you need to fix that first.
When it's valid parse it with JSON.parse.
When it's parsed you'll have a nested array data structure. You need to access the links array by navigating through the tree with the array indexes.
// Returns an array
data.list[0]
// Get the second element of that array
// (the first element is the text "List of recipes")
// which is also an array
data.list[0][1]
// Access the first element of that array
// (an object) and return the links array
data.list[0][1][0].links
const json = '{"list":[["List of recipes",[{"name":"Eggs","links":[{"method":"Fry","link":"https://example.com/category/fry"}]}]]]}';
const data = JSON.parse(json);
console.log(data.list[0][1][0].links);

Related

javascript array to list of array [closed]

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 have a following data, and I want to convert to list of array without key.
How do I do it?
Data
[{
"time": "01/13 15:50",
"price": 3000,
"changeRate": -0.27
},
{
"time": "01/13 15:40",
"price": 4000,
"changeRate": -0.27
}]
Desired output
[
[01/13 15:50, 3000],
[01/13 15:40, 4000]
]
Thank you in advance!
You can use the map method for that. It's used to transform (or "map") one array into a new one. The new array has the same length as the original, and its items are derived from the items in the original array.
data.map(item => [item.time, item.price]);

How to update large JSON objects? [closed]

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 4 months ago.
Improve this question
The server sends me a data as JSON to descripe a product. A product consists of properties and nested arrays of properties, up to 4 level deep. In the Frontend the user can update deep nested values. Do I need to keep track of the path an then reconstruct the whole JSON object for my updating POST call? Or is there a more elegant way to do it? Or is the design of the backend service bad?
data as JSON
{
"productId": 10,
"features": [
{
"id": 45,
"name": "Customization",
"productOptions": [
{
"id": 1,
"color": ["red", "green"],
},
{
"id": 2,
"slogans": [
{"id": 32, "name":"Thank You"},
{"id": 33, "name":"Thanks a lot"},
],
}
]
}
]
}
input to edit slogan
<input data-id="32" type="text" name="slogan" />
edit JSON to send back to the server
const update = {"id": 32, "name":"Thank You so much!"}
// update slogan array
solgansUpdateIndex = slogans.findIndex(obj => obj.id == 32)
slogans[solgansUpdateIndex] = update
...
// update whole object for the updating POST call
{...data, //updateTree}
You should not need to send the whole object back to the server. What you should do is send a set of ids that uniquely identify which field should be updated.
Example: Let's take your data where productId is 10 and featureId is 45
You can create a controller in the backend such as updateProductSlogan(int productId, int featureId, int sloganId, String/Slogan newSlogan).
And then what this endpoint should do is get the corresponding slogan from your database with a query, using these ids and update that slogan with value of newSlogan param.
Note: If only field that contains slogans is the features field you can also hardcode that and change the method to updateProductSlogan(int productId, int sloganId, String/Slogan newSlogan). in which you will fetch the features property with no need to use an id for.

Removing Object from Array of Array in Javascript [closed]

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 10 months ago.
Improve this question
Hey Guys I am exporting some data.
And the challenge I am facing is the data should be in Array of Objects but don't know the data format is Array in Array of Objects.
I can write a script to transform the data to the desired format.
Can anyone help me out with how can I remove the object from the inner array to the outer array?
The data is in this format.
{
"data": [
[
{
"id": "4101"
}
]
]
}
My Resultant Format looks like this
Can anyone help me write a javascript to accomplish this objective
{
"data": [
{
"id": "4104",
}
]
}
Please give a try to this :
var a=obj.data.splice(0, 1);
obj.data.push(a[0]);
or in one line if you want :
obj.data.push(obj.data.splice(0, 1)[0]);
Provided you cannot correct the input at the source.

How to get data in object without looping [closed]

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
I want to get data from the object without for loop
this is my object
data = [{name:"A",value:[java:45,c++:50]},{name:"B",value:[java:12,c++:47]},{name:"C",value:[java:15,c++:32]}]
Expected result:
result_name = ["A","B","C"]
result_java = [45,12,15]
The structure of your array data is invalid. You wrote nested array value in square brackets yet the items are written as key-value pairs.
There is no magical way you can obtain multiple data from an array without iteration/looping. (unless you already know the indices, but this defeats the purpose of using arrays)
That said, if you meant to avoid only the "for" loops, you can:
Fix your data as below.
let data = [
{name:"A", value:{java:45,"c++":50}},
{name:"B", value:{java:12,"c++":47}},
{name:"C", value:{java:15,"c++":32}}
];
Run an alternative loop such as below.
console.log(data.map(item => item.name)); // ["A", "B", "C"]
console.log(data.map(item => item.value.java)); // [45,12,15]

how to use typescript to get the length of an array of objects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to get the length of an an array of objects from the back-end by using API call.
Here is what a "zone" objective looks like:
[
{
"id": 1,
"name": "a"
},
{
"id": 2,
"name": "b"
},
{
"id": 3,
"name": "c"
},
{
"id": 4,
"name": "d"
}
]
and the following typescript code doesn't return the length correctly:
var lengthZones= Object.keys(this.swimbandsService.getZones()).length;
What is the correct way to get the length of an an array of objects in typescript?
I'm guessing you need to wait for the Promise to resolve first, then you can access the response and count the objects inside the array.
this.swimbandsService.getZones().then((response) => {
// might need to parse the response as json, depending on your service logic
var lengthZones = response.length;
});

Categories

Resources