Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
How to Compare Multiple Objects ( Need to match some keys value )

I have an array of objects.

let filterval = [
    {
        'tran_type': "cs+",
        'date': 2/2/2022,
        'amount' 10000
    },
    {
        'tran_type': "cs-",
        'date': 2/2/2022,
        'amount' 10000
    },
    {
        'tran_type':"cs+",
        'date': 3/2/2022,
        'amount' 20000
    },
    {
        'tran_type': "opo",
        'date': 2/2/2022,
        'amount' 10000
    },{
        'tran_type': "opi",
        'date': 2/2/2022,
        'amount' 10000
    },{
        'tran_type': "cs+",
        'date': 1/2/2022,
        'amount' 70000
    },
    {
        'tran_type': "opo",
        'date': 1/2/2022,
        'amount' 90000
    }
]

I need to omit the object if tran_type is cs+ and cs- have the same date and same amount. and if tran_type is opo and opi have the same date and same amount.

I tried my knowledge in javascript. but it does not work properly.


Object.keys(filterval).map( data, index =>{
    if(data.tran_type == 'cs+'){
        Object.keys(filterval).map( data1 =>{
            if(data1.tran_type == 'cs-'){
                if(data1.date == data.date){
                    if(data1.amount == data.amount){
                        data.splice(index, 1)
                    }
                }
            }
        }
    }
    if(data.tran_type == 'opo'){
        Object.keys(filterval).map( data1 =>{
            if(data1.tran_type == 'opi'){
                if(data1.date == data.date){
                    if(data1.amount == data.amount){
                        data.splice(index, 1)
                    }
                }
            }
        }
    }
  });

  console.log(filterval)

Please help me to get the below output. Also, correct my mistake in my code. Solutions are welcome. Thanks in advance.

[
    {
        'tran_type':"cs+",
        'date': 3/2/2022,
        'amount' 20000
    },
    {
        'tran_type': "cs+",
        'date': 1/2/2022,
        'amount' 70000
    },
    {
        'tran_type': "opo",
        'date': 1/2/2022,
        'amount' 90000
    }
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to correct these lines in your code -

1.) 'amount' : 1000 (colon is missing) --- You can use an editor like VS code. It's a good practice and highly recommended. If you will write this code on Vs code editor, you will easily get to know all the syntax error your code has.

2.) filterval is an array and not an Object. So you don't have to use Object.keys. You can use map() directly on filterval.

filterval.map(() => {}) 

Do checkout console.log(Object.keys(filterval)) --- It will print an array of indexes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

3.) data.splice(index, 1) --- splice should be used on arrays and not on object. It should be filterval.splice(index, 1).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

I would recommend that before using any built in methods of JavaScript, do refer MDN docs. Also, it's a good practice to break down your code into smaller chunks and keep checking at every step, if everything is going on right or not. If everything seems fine then move to the next step. If something's wrong then correct it and then move ahead.

Here's the code that fulfills the purpose.

let filterval = [
{
    'tran_type': "cs+",
    'date': "2/2/2022",
    'amount' : 10000
},
{
    'tran_type': "cs-",
    'date': "2/2/2022",
    'amount' : 10000
},
{
    'tran_type':"cs+",
    'date': "3/2/2022",
    'amount' : 20000
},
{
    'tran_type': "opo",
    'date': "2/2/2022",
    'amount' : 10000
},
{
    'tran_type': "opi",
    'date': "2/2/2022",
    'amount' : 10000
},
{
    'tran_type': "cs+",
    'date': "1/2/2022",
    'amount' : 70000
},
{
    'tran_type': "opo",
    'date': "1/2/2022",
    'amount' : 90000
}]



filterval.forEach((data1, index1) => {
    switch (data1.tran_type) {
        case 'cs+' :             
            removeIfFound(data1, index1, 'cs-')
            break;

        case 'cs-' :
            removeIfFound(data1, index1, 'cs+')
            break;

        case 'opo' :
            removeIfFound(data1, index1, 'opi')
            break;

        case 'opi' :
            removeIfFound(data1, index1, 'opo')
            break

        default : 
            break
    }
  })

  function removeIfFound(data1, index1, trans_type) {
    filterval.forEach((data2, index2) => {
        
        if (data2.tran_type === trans_type && checkIfEqual(data1, data2)) {
            filterval.splice(index1, 1)
            filterval.splice(index2 - 1, 1) 
            return
        }
    })
  }

  function checkIfEqual(data1, data2) {
    return (data1.date === data2.date) && (data1.amount === data2.amount)
  }

  console.log(filterval)

Let me know in case you need any explanation.

about 4 years ago · Juan Pablo Isaza Report

0

Change :

if(data.tran_type = 'cs+'){

code above always return true because you are giving data.tran_type value of 'cs+' to

if(data.tran_type == 'cs+'){

and also

'amount' 10000 //should be :
'amount': 10000 // you don't have ':'

and this code is checking if data.tran_type is equal to the value of 'cs+'.

and try to write your logic better, having so much ifs one inside another is very bad practice, it is difficult to read and see what the code does, try to make smaller functions which you call if some condition is accomplished.

about 4 years ago · Juan Pablo Isaza Report

0

Soooo, I broke your code in chunks and got the result you were asking for, this is not the best-looking code, but I wrote it so you can go through it and try to understand it

let array = [
  {
    tran_type: "cs+",
    date: 2 / 2 / 2022,
    amount: 10000,
  },
  {
    tran_type: "cs-",
    date: 2 / 2 / 2022,
    amount: 10000,
  },
  {
    tran_type: "cs+",
    date: 3 / 2 / 2022,
    amount: 20000,
  },
  {
    tran_type: "opo",
    date: 2 / 2 / 2022,
    amount: 10000,
  },
  {
    tran_type: "opi",
    date: 2 / 2 / 2022,
    amount: 10000,
  },
  {
    tran_type: "cs+",
    date: 1 / 2 / 2022,
    amount: 70000,
  },
  {
    tran_type: "opo",
    date: 1 / 2 / 2022,
    amount: 90000,
  },
];

let finalArray = [];

let csPositive = [];
let csNegative = [];
let opo = [];
let opi = [];

array.map((element) => {
  sortByType(element);
});

function sortByType(element) {
  sortCsPositive(element);
  sortCsNegative(element);
  sortOpo(element);
  sortOpi(element);
}

function sortCsPositive(element) {
  if (element.tran_type == "cs+") {
    csPositive.push(element);
  }
}
function sortCsNegative(element) {
  if (element.tran_type == "cs-") {
    csNegative.push(element);
  }
}
function sortOpo(element) {
  if (element.tran_type == "opo") {
    opo.push(element);
  }
}
function sortOpi(element) {
  if (element.tran_type == "opi") {
    opi.push(element);
  }
}

function compareDateAndAmout(oneArray, secondArray) {
  oneArray.forEach((x) => {
    secondArray.forEach((y) => {
      y.date == x.date && x.amout == y.amout ? "" : finalArray.push(x);
    });
  });
}

compareDateAndAmout(opo, opi);
compareDateAndAmout(csPositive, csNegative);
console.log(finalArray);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!