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 3619366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:52:10+00:00 2026-05-18T22:52:10+00:00

I have a JavaScript object as follows: var data = {items: [ {id: 1,

  • 0

I have a JavaScript object as follows:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

If I wanted to add/remove items to this list, how would I go about it using jQuery? The client wants this list to be dynamically modifiable.

  • 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-05-18T22:52:10+00:00Added an answer on May 18, 2026 at 10:52 pm

    First off, your quoted code is not JSON. Your code is JavaScript object literal notation. JSON is a subset of that designed for easier parsing.

    Your code defines an object (data) containing an array (items) of objects (each with an id, name, and type).

    You don’t need or want jQuery for this, just JavaScript.

    Adding an item:

    data.items.push(
        {id: "7", name: "Douglas Adams", type: "comedy"}
    );
    

    That adds to the end. See below for adding in the middle.

    Removing an item:

    There are several ways. The splice method is the most versatile:

    data.items.splice(1, 3); // Removes three items starting with the 2nd,
                             // ("Witches of Eastwick", "X-Men", "Ordinary People")
    

    splice modifies the original array, and returns an array of the items you removed.

    Adding in the middle:

    splice actually does both adding and removing. The signature of the splice method is:

    removed_items = arrayObject.splice(index, num_to_remove[, add1[, add2[, ...]]]);
    
    • index – the index at which to start making changes
    • num_to_remove – starting with that index, remove this many entries
    • addN – …and then insert these elements

    So I can add an item in the 3rd position like this:

    data.items.splice(2, 0,
        {id: "7", name: "Douglas Adams", type: "comedy"}
    );
    

    What that says is: Starting at index 2, remove zero items, and then insert this following item. The result looks like this:

    var data = {items: [
        {id: "1", name: "Snatch", type: "crime"},
        {id: "2", name: "Witches of Eastwick", type: "comedy"},
        {id: "7", name: "Douglas Adams", type: "comedy"},     // <== The new item
        {id: "3", name: "X-Men", type: "action"},
        {id: "4", name: "Ordinary People", type: "drama"},
        {id: "5", name: "Billy Elliot", type: "drama"},
        {id: "6", name: "Toy Story", type: "children"}
    ]};
    

    You can remove some and add some at once:

    data.items.splice(1, 3,
        {id: "7", name: "Douglas Adams", type: "comedy"},
        {id: "8", name: "Dick Francis", type: "mystery"}
    );
    

    …which means: Starting at index 1, remove three entries, then add these two entries. Which results in:

    var data = {items: [
        {id: "1", name: "Snatch", type: "crime"},
        {id: "7", name: "Douglas Adams", type: "comedy"},
        {id: "8", name: "Dick Francis", type: "mystery"},
        {id: "4", name: "Ordinary People", type: "drama"},
        {id: "5", name: "Billy Elliot", type: "drama"},
        {id: "6", name: "Toy Story", type: "children"}
    ]};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In javascript I have an array as follows: var foo = [2, 2, 4,
Just wonder. I have extended javascript Array object using its prototype as follows: <html>
I have a Javascript object like this: var count = { table:19, people:39, places_details:84,
I have a rails template (.rhtml file) generating a Javascript object. It looks something
There have been some questions about whether or not JavaScript is an object-oriented language.
I'm creating my own JavaScript Array-like object and I have methods that call closures.
In Javascript, I have an object: obj = { one: foo, two: bar };
I have a JavaScript object that I'd like to add some properties to, but
I have a custom Javascript object that has a few string and float members.
I have a JavaScript method that I need to run on one of my

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.