0

I have an object like this :

let data = {
    approval: [{
            name: 'XXXX',
            kode: '10'
        },
        {
            name: 'YYYY',
            kode: '12'
        },
        {
            name: 'ZZZZ',
            kode: '14'
        },
        {
            name: 'HHHH',
            kode: '09'
        }

    ],
    batas: "10"
}

I want to display the data that is equal to this condition (kode >= 10) so if the 'kode' is less than 'batas' it shouldn't appear, any help?

So in that case the output should be like this

approval: [{
        name: 'XXXX',
        kode: '10'
    },
    {
        name: 'YYYY',
        kode: '12'
    },
    {
        name: 'ZZZZ',
        kode: '14'
    }

]

the data comes from ajax, Thanks for your help.

1
  • 1
    simple array filter should do the trick Commented Nov 10, 2022 at 1:24

4 Answers 4

1

I made the es6 javascript code.

First I put the approval array and batas into the variable first.

Then I use for loop iteration to find data less than 10, then I delete it using splice.

The last time I returned it was an object

This is my code :

    const batas = (data) => {
        let arr = data.approval
        let batas = data.batas
        for (let i = 0; i < arr.length; i++) {
            if (arr[i]['kode'] < batas) arr.splice(i, 1)
        }
        return data
    }
    
    let data = {
        approval: [
            {
                name: 'XXXX',
                kode: '10'
            },
            {
                name: 'YYYY',
                kode: '12'
            },
            {
                name: 'ZZZZ',
                kode: '14'
            },
            {
                name: 'HHHH',
                kode: '09'
            }
        ],
        batas: '10'
    }
    
    console.log(batas(data))

Sign up to request clarification or add additional context in comments.

Comments

0

These are some of the many ways to do what you want:

    let data = {
            approval: [
                {
                    name: 'XXXX',
                    kode: '10'
                },
                {
                    name: 'YYYY',
                    kode: '12'
                },
                {
                    name: 'ZZZZ',
                    kode: '14'
                },
                {
                    name: 'HHHH',
                    kode: '09'
                }
                
            ],
            batas: "10"
        }

    // solution 1 > use for loop:
    const results_1 = [];
    for (let i=0; i<data.approval.length; i++) {
      if (data.approval[i].kode >= data.batas) results_1.push(data.approval[i]);
    }
    console.log(results_1);
    
    // solution 2 > use for...of loop:
    const results_2 = [];
    for (const obj of data.approval) {
      if (obj.kode >= data.batas) results_2.push(obj);
    }
    console.log(results_2);

    // solution 3 > use filter method:
    const results_3 = data.approval.filter(obj => obj.kode >= data.batas);
    console.log(results_3);

Comments

0

Using filter method can do this, for more info check: MDN - filter.

let data = {
        approval: [
            {
                name: 'XXXX',
                kode: '10'
            },
            {
                name: 'YYYY',
                kode: '12'
            },
            {
                name: 'ZZZZ',
                kode: '14'
            },
            {
                name: 'HHHH',
                kode: '09'
            }
            
        ],
        batas: "10"
    }

const filteredData = {approval: data.approval.filter(item => item.kode >= data.batas)}

console.log('filteredData: ', filteredData)

Comments

0

Use Array#filter method as follows:

const
    data = { approval: [ { name: 'XXXX', kode: '10' }, { name: 'YYYY', kode: '12' }, { name: 'ZZZZ', kode: '14' }, { name: 'HHHH', kode: '09' } ], batas: "10" },
    
    output = [ data ]
        .map(
            ({approval,batas}) => 
            ({approval:approval.filter(
                ({kode}) => kode >= batas
            )})
        )[0];
        
console.log( output );

However, ....

If all you need is the filtered, array then this approach would suffice:

const
    data = { approval: [ { name: 'XXXX', kode: '10' }, { name: 'YYYY', kode: '12' }, { name: 'ZZZZ', kode: '14' }, { name: 'HHHH', kode: '09' } ], batas: "10" },
    
    output = data.approval
        .filter(
            ({kode}) => kode >= data.batas
        );
        
console.log( output );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.