0

I want to create one Json object from the two arrays using JavaScript or jQuery. The data saved in database in the following format:

clob_field_1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 etc etc ...

clob_field_2: 8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9, 8265.81, 8319.5, 8186.43 etc etc ...

Ideally, the outcome should looks like this:

[{"Item:" 1, "Value:" 8106.23}, {"Item:" 2, "Value:" 7856.49}, {"Item:" 3, "Value:" 8009.15}, {"Item:" 4, "Value:" 8121.78}, etc etc ...]

So what I've done, I've fetched the clobs fileds from the database using PL/SQL, so I can access it from the JavaScript. After that I declared 2 variables and set the object and want to loop through the data to create a nice pairs of data. This is my JavaScript code so far:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9,    8265.81, 8319.5, 8186.43];
  var obj = {};
for (var i = 0; i < a.length; i++) {
  obj['Item:' + a[i]] = 'Value:' + b[i] + '}';
}
alert(JSON.stringify(obj));

But the outcome is awful and looks as follows:

{"Item:1":"Value:8106.23}","Item:2":"Value:7856.49}","Item:3":"Value:8009.15}","Item:4":"Value:8121.78}","Item:5":"Value:8082.8}","Item:6":"Value:8294.43}","Item:7":"Value:8137.9}","Item:8":"Value:8265.81}","Item:9":"Value:8319.5}","Item:10":"Value:8186.43}"}

The left curly brackets are missing from the pair 2 onwards, colons instead of coma, speech marks are in wrong place etc etc ... I believe there is a syntaxis error in my code and I cannot figure out what is it. Any help appreciated, Thanks in advance!

5
  • [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] why would you want something like that? Why not simply var a = 10; Commented Aug 29, 2016 at 12:22
  • In your code you have only the closing + '}' why? I mean why do you use it at all? Commented Aug 29, 2016 at 12:24
  • what is your expected output ? Commented Aug 29, 2016 at 12:25
  • You want an array in output and you just intiaized an object and that object is globally intialized for loop which is not a right justice with question Commented Aug 29, 2016 at 12:30
  • 1
    It's important that you realise that you should only care about building the list + objects. There is no reason for you to try and inject curly brackets or even colons into the objects. Commented Aug 29, 2016 at 12:31

5 Answers 5

3

A compact way via map and ES6 syntax:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9, 8265.81, 
         8319.5, 8186.43];
var obj = a.map( (item, i) => ({ item, value: b[i] }) );
console.log(obj);

Note: I would advise against capitalising the first letter of property names. It is a common practice to only do this for constructor functions.

Sign up to request clarification or add additional context in comments.

3 Comments

you could make it a bit more compact with var obj = a.map((item, i) => ({ item, value: b[i] }));
Thanks @NinaScholz, you are right. NB: I see we are often looking at the same questions :) I like your contributions!
The reason I keep it capital, that I'm gonna create a chart using this data and I have to keep capital. But thanks for your comments, will keep it my mind in the future. Thanks!
1

Create an array for your output and push new objects into it rather than setting them all in a single object.

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9,    8265.81, 8319.5, 8186.43];
var output = [];
for (var i = 0; i < a.length; i++) {
    output.push({'Item': a[i], 'Value': b[i]});
}
alert(JSON.stringify(output));

2 Comments

Works as a charm! Thank you!
Thanks to all of you guys, I will mark 'Kaivosukeltaja' answer as correct as it best suit to my requirements. But again, thanks to all of you, really appreciated your help guys ! And thanks for pointing me what was the problem with my code !
1

Can you try:

var obj = [];
for (var i = 0; i < a.length; i++) {
  obj.push({
     Item: a[i],
     Value: b[i]
  });
}

1 Comment

Thanks, your code works as well ! Thanks for your help !
1
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9,    8265.81, 8319.5, 8186.43];

  var objArra = [];
for (var i = 0; i < a.length; i++) {
   var obj = {};

  obj['Item:'] = a[i];
  obj['value:'] = b[i];
  objArra.push(obj)
}
alert(JSON.stringify(objArra));

Fiddle Check

1 Comment

Thanks, that also works, but I marked 'Kaivosukeltaja' answer as correct as it best suit my requirements. Thanks for your help anyways and it was helpfull too !
1

if you suppose, that your inputs will be right, you could also try this

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [8106.23, 7856.49, 8009.15, 8121.78, 8082.8, 8294.43, 8137.9,    8265.81, 8319.5, 8186.43];
let sum = a.map((val, index) => {
    return {'Item':val, 'Value': b[index]};
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.