0

I am very new to LWC and javascript.I have a requirement as follows:

Declare array of object which represents employee (name,salary,position) loop the employee array and display the employee information. create input box to enter the salary amount. display only the objects above the salary entered in the input box.

I tried like below code: but getting error. can anyone pls help

js

Employees=[
    {
        name: 'E1',
        salary: 20000,
        position: 'p1',
    },
    {
        name: 'E2',
        salary: 50000,
        position: 'p2',
    },
    {
        name: 'E3',
        salary: 100000,
        position: 'p3',
    },
    {
        name: 'E4',
        salary: 1000000,
        position: 'p4',
    },
    {
        name: 'E5',
        salary: 7000,
        position: 'p5',
    }
    
]
empsal
newArray=[]
empdetails(event){
    this.empsal=parseInt(event.target.value)
   this.newArray=this.Employees.filter((employee)=>employee.salary>this.empsal)
console.log(newArray)
}

html

<template>
<lightning-input label="Enter the salary" onchange={empdetails}></lightning-input>
{newArray}
</template>

2 Answers 2

0

The error was actually your console.log line. You forgot to include this, so it tried to access the top-level scope for newArray, which isn't defined, thus the error. You can fix that with:

console.log(this.newArray);

Note that your current code will show only [object Object], because of how toString works; you'll want to set up a table to see all the values.

Demo.

0
  1. @track is the decorator that helps us to track a private property's value and re-render a component when it changes.
  2. Its helps us to achieve two-way data binding.

For more you can refer : https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .