Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:38:07+00:00 2026-06-16T06:38:07+00:00

I want to create a JSON object words which further contains a list of

  • 0

I want to create a JSON object words which further contains a list of
objects s and e with different values as follows:

words = {
    "a1": { s: 1.5, e: 2 },
    "a2": { s: 2.1, e: 2.4 },
    "a3": { s: 2.5, e: 3 },
    "a4": { s: 3.5, e: 3.8 },
    "a5": { s: 3.9, e: 4.5 },
    "a6": { s: 4.8, e: 5.2 },
    "a7": { s: 5.3, e: 5.3 },
    "a8": { s: 6.5, e: 7 },
    "a9": { s: 7.2, e: 8 },
    "a10": { s: 8.1, e: 8.4 }
};

I need to add these values using a for loop. I have applied the
following solution for this:

var words = {};
var xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];
words = '{';
for (var i = 0; i <= 10; i++) {
    if (i == 10)
        var obj = '\"a' + i + '\"'+': { s:' + xyz[i] + ', e:' + xyz[i + 1] + '}';
    else
        var obj = '\"a' + i + '\": { s:' + xyz[i] + ', e:' + xyz[i + 1] + '},';

    words = words + obj;
}
words = words + '}';

$.parseJSON(words);

But this generates an error of:

Uncaught SyntaxError: Unexpected token s**

I am not able to create the object due to this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:38:08+00:00Added an answer on June 16, 2026 at 6:38 am

    You don’t need to create a JSON string, then parse it with JS: it’s far more easily done with this:

    var words = {},
        xyz = [1.5,2,2.1,2.4,2.5,3,3.5,3.8,3.9,4.5,4.8,5.2,5.3,5.3,6.5,7,7.2,8];
    
    for (var i = 0, l = xyz.length/2; i < l; i++) {
      words['a' + (i + 1)] = { 
        s: xyz[2*i], 
        e: xyz[2*i + 1] 
      };
    }
    
    console.log(words);
    

    Explanation: you go through your source arrays, taking each pair into an object literal and assign it to words object with bracket notation (so that value of 'a' + (i+1) is evaluated as a new key).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create JSON object using tow query in PHP , Then retrieve
I want to create a JSON object from my MySQL results with PHP so
I have a list, which may be empty or non-empty. I want to create
I want to create json object from a string that comes as a response
Good day! I want to create a function wherein I need to use JSON
i want create a custom json data from the mssql 2008 results so that
Hi I want to create a JSON array. I have tried using: JSONArray jArray
This the model that I want to create using json file Ext.define('Users', { extend:
I want to create a .NET webservice using C# with JSON. but I've error
I want to be able to dynamically create ViewControllers based on a JSON file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.