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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:10:04+00:00 2026-05-22T21:10:04+00:00

On JavaScript, I have the following JSON: var mJSON = { monster:[ {id:150,name:Richard}, {id:100,name:Gregory},

  • 0

On JavaScript, I have the following JSON:

var mJSON = {
    "monster":[
        {"id":"150","name":"Richard"},
        {"id":"100","name":"Gregory"},
        {"id":"200","name":"Rachel"},
        {"id":"250","name":"Mike"}
    ]
}

I need to refine this object by a string inputted by the user. For example: “100”.
The result should be a new JSON like this:

var zJSON = {
    "monster":[
        {"id":"100","name":"Gregory"}
    ]
}

I tried looking in Google on easy ways to run through a JavaScript object searching for a string, but without success. There’s nothing like jQuery’s $.inArray too, as far I know. Anyone has any idea?
I’m thinking about converting this JSON into a string, grep it for the value inputted by the user, and then converting the string to JSON again, but I think this will be too troublesome for something that could be easy to achieve.

  • 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-22T21:10:05+00:00Added an answer on May 22, 2026 at 9:10 pm

    How about using $.map?

    var id = 100;
    var result = $.map(monsters, function(monster){
        return monster.id == id ? monster : null;
    });
    

    JQuery.map() applies function to each argument of the array (monsters) and produces the new array that contains the values returned by the function. What is important in this case is that if function returns null then the element is removed from the resulting array.

    EDIT:
    As @Jan has kindly suggested in his comment $.grep suits even better! Here is the code example for your monsters:

    var id = 100;
    var result = $.grep(monsters, function(monster){
        return monster.id == id;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JavaScript array of real estate home objects: var json =
I have the following javascript: $.ajax({ type: "POST", dataType: "json", url: "/Home/Submit", data: {
I have the following JavaScript code: oCoord = {x: null, y: null}; var aStack
I have the following JavaScript program saved in a file pre.js : var pre
I have the following JSON and Jquery Code: JSON { employees:[ { name: Sandy
I have the following javascript object containing a multi-value email property: var contact =
I have the following json output when i call a sample webservice <string>[{Name:Ajay Singh,Company:Birlasoft
I have the following javascript code: var groupArray = []; groupArray.push($(#group).val()); var sendInfo =
I have the following JSON: var json = { system : { world :
I have the following javascript code: function initSite(){ var site; $.getJSON(www+'init/initSite', function(data) { site

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.