Removing Object from Array of Array in Javascript [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 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.

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]);

JSON jQuery auto-fill [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 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);

How to push() things into an array with 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 1 year ago.
Improve this question
I want to push an item into an array that in that have objects in them, like this:
var testarray = [
{
first: "See this?",
second: "Oh my!"
},
{
first: "Nice!",
second: "Amazing!"
}
]
You can see that I for this variable/array, I can't just use this testarray.push("Wonderful!"). Please show me how I can push things into these sort of arrays.
The "multiple things" are objects. Push another object in the same format.
testarray.push({first: "Wow!", second: "Wonderful!"});

Why does JSON.parse() add numbers in front of elements? [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 years ago.
Improve this question
I have a string where I build my build my "json-like" format, like
_toBeFormated =
[
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]},
{"foor":"bar","foo":"bar","foo":["bar,bar"]}
]
But after calling JSON.parse like _afterFormat = JSON.parse(_toBeFormated), my structure looks like the following:
_afterFormat =
0:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
1:{"foor":"bar","foo":"bar","foo":["bar,bar"]},
2:{"foor":"bar","foo":"bar","foo":["bar,bar"]}
If I try to change to JSON Format at the beginning, like leaving out [ ], if failes to parse, also it looks like valid JSON to me. What am I missing, or why does it add the numbers at the beginning?
It doesn't add numbers. The data structure is an array. The tool you are using to look at the array is showing the index of each entry.

How to add data to the child node of a json [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 7 years ago.
Improve this question
I want to add data {"address" : "http://ebay.com"} to the second child of this json data i.e in the bracket with the data "api" : "Ebay".
How can I do that with jQuery?
{"Amazon":[
{"price":"19,799.00","delivery":"FREE","api":"Amazon"},
{"price":"29,999","delivery":"FREE","api":"Ebay"}
]
}
Try,
var obj = {"Amazon":[ {"price":"19,799.00","delivery":"FREE","api":"Amazon"},{"price":"29,999","delivery":"FREE","api":"Ebay"} ] };
obj.Amazon[1].address = "http://ebay.com";
or use $.merge(obj1,obj2) function
var obj = {"Amazon":[ {"price":"19,799.00","delivery":"FREE","api":"Amazon"},{"price":"29,999","delivery":"FREE","api":"Ebay"} ] };
$.merge(obj.Amazon[1], {
address: "http://ebay.com"
});
DEMO

Categories

Resources