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

  • Home
  • SEARCH
  • 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 8186319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:09:25+00:00 2026-06-07T02:09:25+00:00

In JavaScript I am filling array elements by traditional way, such as: myArray[12] =

  • 0

In JavaScript I am filling array elements by traditional way, such as:

myArray[12] = 123;
myArray[13] = 432;
myArray[14] = 233;
myArray[15] = 98;

Is it possible to do this faster by one command?

For example to put numbers [123, 432, 233, 98] to myArray[12-15] … somehow? Not one by one.


And how can be optimized this kind of scenario:

newArray[12] = oldArray[30];
newArray[13] = oldArray[31];
newArray[14] = oldArray[32];
newArray[15] = oldArray[33];

when n elements (4 in the above example) next each to other from one array need to be moved to another array?

Please advice.

  • 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-07T02:09:28+00:00Added an answer on June 7, 2026 at 2:09 am
    var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
                   12, 13, 14, 15, 16, 17, 18, 19, 20];
    myArray.splice(12, 4, 123, 432, 233, 98);
    
    // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
    //  123, 432, 233, 98, 16, 17, 18, 19, 20]
    

    The first parameter to splice is the index to start with, the second is the number of elements to remove, the remainder are elements to insert. So if you insert the same number as were removed, you end up replacing them.

    If you do want this to come from another array, you could try this, although it is not as clean:

    var oldArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
                   12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
                   22, 23, 24, 25, 26, 27, 28, 29, 123, 
                   432, 233, 98, 34, 35]
    var newArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
                   12, 13, 14, 15, 16, 17, 18, 19, 20];
    
    [].splice.apply(newArray, [12, 4].concat(oldArray.slice(30, 30 + 4)));
    
    // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
    // 123, 432, 233, 98, 16, 17, 18, 19, 20]
    

    This is probably less efficient than PointedEars suggestion but it’s quite simple and would probably serve for many use cases.

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

Sidebar

Related Questions

Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have an array: myarr = []; I'm filling it with some values: myarray['name']
Possible Duplicate: JavaScript: formatting number with exactly two decimals Maybe I'm going about this
I assume this one is rather a JavaScript/jQuery/DOM question than a Rails question, but
I am filling dropdownlist with javascript. for (var i = 1; i < array.length;
We're writing a web app that relies on Javascript/jQuery. It involves users filling out
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
My GWT JavaScript app prints reports by opening a new browser window programmatically, filling
I'm quite new with javascript and I don't understand this problem: $(function() { var
I'm dynamically creating some <div> elements and then filling their innerHTML properties with text.

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.