Extract data from JSON object

My JSON object is called idAndNames.json;

[

  { "id":"1", "name":"name1"},

  { "id":"2", "name":"name2"},

  { "id":"3", "name":"name3"}

]

I'm looking to filter it by ID and name

function applyFilter(id, valueItem) {

  return id <= valueItem;

}
//Using Immutable.js, I converted the JSON to an array -> fromJS(idAndNames);

idAndNamesArray.filter(item => applyFilter(item.get('id'), valueItem));

However, I'm facing issues filtering by ID or name

For instance, when valueItem=1 as an integer, I receive the error: "Expected Array or iterable object of values"

On the other hand, when valueItem="1" as a string, there is no error but the filter doesn't work

Could you please assist me in finding the correct approach? Thank you

Answer №1

I have tested your code in a fiddle and confirmed that it works with both integers and strings: https://jsfiddle.net/nyvbb4nd/1/

Have you properly declared the variable valueItem in your code?

var valueItem='1'; //1
var result = idAndNamesArray.filter(item => checkFilter(item.get('id'),valueItem));
console.log(result);

Answer №2

If you have been relying solely on Immutable for this particular task, it might be a good idea to switch to using ES6 syntax instead:

To enhance the functionality of `checkFilter`, I've made adjustments so that it now returns a function that can operate on each individual filtered element. Simply provide `valueItem` as the parameter.

const checkFilter = (valueItem) => (el) => el.id <= valueItem;
let result = data.filter(checkFilter('2'));

Check out the DEMO

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process of creating Vue.js single file components and incorporating them into a non-single page application?

Currently, I am in the process of developing a web application (specifically ASP.NET Core 2 MVC) that is not a single page application. Vue.js is being used to enhance the front-end functionality through a Vue instance. As the project progresses, there is ...

Implementing slideDown() functionality to bootstrap 4 card-body with jQuery: A step-by-step guide

Here is the unique HTML code I created for the card section: <div class="row"> <% products.forEach(function(product){ %> <div class="col-lg-3 col-md-4"> <div class="card mb-4 shadow "> &l ...

Generating JSON Data with the Power of JavaScript and JQuery

Looking to dynamically generate a JSON Object with the specified structure: { "deleteId":[1,2,3], "pointId":[1,2,3], "update":[ { "what":"mission", "id":1, "value":"adsda" }, { ...

I need to obtain the URL pathname on a server component in Next.js version 13 - how is this achieved

I'm facing an issue with retrieving the pathname of a server component located in the app directory. Despite attempting to obtain it through window.location, I haven't been successful. Is there an alternative method I can use to achieve this? ...

What is the procedure for a parent component to transmit HTML to a child component within Angular?

How can a parent component pass multiple ng-templates to a child component? For example, the parent component includes multiple ng-templates: <app-childcomponent> <ng-template>Item A</ng-template> <ng-template>Item B</n ...

Exploring the indexOf method in Jade

Currently using Jade and Express. '#{value.users}' is an array data type. '#{user.username}' is a string data type. Attempting to utilize the if '#{value.users}'.includes('#{user.username}') method. When true, I ...

JavaScript-generated buttons fail to trigger VueJS functions

I have a function that creates buttons for each item in a list, and each button is supposed to execute a function in the App.vue file when clicked. The issue I am facing is that neither the onclick nor the v-on:click methods are functioning as expected. ...

An error stating that "DataTable is not a recognized function" occurred within the document function

Previously, I set up datatables using the code below: $(function () { $('#keywords-table').DataTable({ "ajax": ({ url: "{{ route('getKeywordsByProductId') }}", method: "get", ...

Movement and physics mechanics for players using Physi.js

As I work on a basic game using Three.js for rendering and Physijis for physics, my question can be applied to games in general. In games, players often display movement that appears disconnected from the physics engine. They can accelerate instantly and ...

Having trouble pushing to an array in Angular typescript? It seems to be returning as undefined

As a newcomer to Angular with a basic understanding of JavaScript, I am attempting to create a program that can solve Sudoku puzzles. In a 9x9 grid, there are a total of 81 points or squares. To efficiently check for violations of Sudoku rules - no repeati ...

Can an image map be utilized within an <a> element?

I am attempting to implement an image map within an <a> tag. It seems to be functioning correctly in Chrome, but I am encountering issues with it not working in Internet Explorer. Is it acceptable to use an image map in an <a> tag? Below is th ...

Switch the text display by clicking on a different button in the list

I am currently dealing with an issue involving a list of boxes containing text/images and buttons for expanding/collapsing the text. Whenever I click on another item's button, the text box that was previously opened gets closed, but the button text re ...

Error occurs when deserializing a JSON list as null

Looking at the JSON data provided: json = <json>{ "Header": { "MCC": "415", "FO": "0", "REGID": "2" }, "Contacts": [ { "mo": "70875623", "name": "salam" }, { "name": "salam2", "mo": "70242 ...

Displaying JSON Array in ListView - Merging

Although this question has been asked before, my code has some variations which are causing confusion regarding where I need to make changes. My goal is to extract data ("fest_name") and store it in an ArrayList called "festivals", and then display it i ...

Establish a connection between two JSON files using the WordPress REST API

I have developed an app using ionic 2 that revolves around quotes. My goal is to manage these quotes (along with authors, categories, etc) using Wordpress and its REST API. Initially, I utilized normal posts for this purpose, but now I am exploring custom ...

The node experiences a crash when the redis-server goes offline

Struggling with a persistent issue here. Despite reading numerous documents and posts from others on the same topic, I can't seem to find a solution to prevent this problem. I am intentionally shutting down the redis server to avoid potential disaster ...

Jest - experiencing intermittent test failures on initial run, yet succeeding on subsequent runs

Writing tests with jest and supertest npm on a node server has been a challenge for me. When I try to run all the tests together, some of them fail inexplicably: https://i.sstatic.net/TWDVp.png However, if I selectively run only the failed tests in the t ...

The componentDidUpdate method ensures equality between the previous and current states

Within a dashboard, the layout data for each module (utilizing react-grid-layout) is stored separately from the module data and both are saved in a database. The Dashboard component state holds the current module data and layout data. I'm attempting t ...

I'm looking to automate a system response whenever a user inputs something - how can I achieve this using NodeJS and Socket

I'm looking to create a feature where, if a user enters a specific command, the socket.io will respond with a predefined message. For example: Input: !hi Output: hello! If anyone knows how to achieve this, I'd appreciate some guidance, tha ...

Learn how to import a stylus file globally in Vite build tool

Recently, I decided to use vite for building a new vue application. However, I encountered some difficulties when trying to globally import a stylus file in my vite.config.js. I followed the code examples provided in the vite documentation: export default ...