How to add data to the child node of a json [closed] - javascript

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

Related

what is the work of .id in this given function [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 9 hours ago.
Improve this question
what is .id in this given function
function getSelected() {
let answer = undefined;
answerEls.forEach((answerEl) => {
if (answerEl.checked) {
answer = answerEl.id;
}
});
return answer;
}
I tried to find what is .id by console .log but it does does not show any value

can not convert this string to object [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 8 months ago.
Improve this question
I can not convert this string to object.Why I am getting this error? please help.
onSignup(data:any){
localStorage.setItem('users',JSON.stringify(data));
let users = JSON.parse(localStorage.getItem('users'));
}
Try using function onSignup(data: any) { localStorage.setItem('users', JSON.stringify(data)); let users = JSON.parse(localStorage.getItem('users') as string); }
If you are using typescript you can't pass a string | undefined when a string is expected.

In javascript how can I convert multiple arrays of two elements into key-values an object? [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 8 months ago.
Improve this question
I have an array of objects:
arr = [['name', 'jack'],['age', 18],];
How do I convert it into the following by JavaScript?
{ name: 'jack', age: 18 }
arr = [['name', 'jack'],['age', 18],];
const obj = Object.fromEntries(arr);
console.log(obj);
//{
// "name": "jack",
// "age": 18
//}

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.

Getting undefined values from JavaScript array [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 5 years ago.
Improve this question
I am getting response something like this in the form JavaScript Object
{
"result": [
{
"invoice_prefix": "INV",
"maximum_invoice_no": "0009"
}
]
}
I am trying to fetch the value of invoice_prefix and maximum_invoice_no
I have written result[0].maximum_invoice_no but I am getting undefined .
I have tried a lot of other ways also but still getting undefined . Please help me and give me some hint
I tried that and it is working as expected. I hope this will be helpful.
let test = {
"result": [
{
"invoice_prefix": "INV",
"maximum_invoice_no": "0009"
}
]
}
document.getElementById("output").innerHTML = "Maximum Invoice No. : " + test.result[0].maximum_invoice_no;
console.log(test.result[0].maximum_invoice_no);
<p id="output"></p>

Categories

Resources