skip to Main Content

I have an array of Objects (Jerseys) and some are Authentic (true) and some are Replicas (Authentic: False)

const Jerseys = [{"Player":"Shaq","Team":"Miami Heat","Sport":"Basketball","Authentic":true,"Price":325,"Year":2007,"jerseyNum":32},{"Player":"Michael Jordan","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":1992,"jerseyNum":23},{"Player":"Ken Griffey","Team":"Seattle Mariners","Sport":"Baseball","Authentic":true,"Price":300,"Year":1989,"jerseyNum":24},{"Player":"Tim Hardaway","Team":"Miami Heat","Sport":"Basketball","Authentic":false,"Price":135,"Year":1996,"jerseyNum":10},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1994,"jerseyNum":13},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1990,"jerseyNum":13},{"Player":"Tim Anderson","Team":"Miami Marlins","Sport":"Baseball","Authentic":false,"Price":125,"Year":2021,"jerseyNum":15},{"Player":"Will Smith","Team":"Bel-Air","Sport":"Basketball","Authentic":false,"Price":135,"Year":2007,"jerseyNum":41},{"Player":"Pete Rose","Team":"Cinncinatti Reds","Sport":"Baseball","Authentic":true,"Price":300,"Year":1975,"jerseyNum":14},{"Player":"Derrick Rose","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":2008,"jerseyNum":1},{"Player":"Unknown","Team":"Miami Giants","Sport":"Baseball","Authentic":false,"Price":70,"Year":1950,"jerseyNum":8}];

let isReplica = [];
for(let i = 0; i < Jerseys.length;i++){
    if(Jerseys[i].Authentic === false){
    isReplica.push(Jerseys[i]);
    console.log(isReplica);

This code gets me the Replica Jerseys, BUT the array is pushed the length of amount of Replicas. I only want 1 Array. What’s wrong with this logic? I also CANNOT use the FILTER Function (I want to do it manually 1st).

2

Answers


  1.  const Jerseys = [{"Player":"Shaq","Team":"Miami Heat","Sport":"Basketball","Authentic":true,"Price":325,"Year":2007,"jerseyNum":32},{"Player":"Michael Jordan","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":1992,"jerseyNum":23},{"Player":"Ken Griffey","Team":"Seattle Mariners","Sport":"Baseball","Authentic":true,"Price":300,"Year":1989,"jerseyNum":24},{"Player":"Tim Hardaway","Team":"Miami Heat","Sport":"Basketball","Authentic":false,"Price":135,"Year":1996,"jerseyNum":10},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1994,"jerseyNum":13},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1990,"jerseyNum":13},{"Player":"Tim Anderson","Team":"Miami Marlins","Sport":"Baseball","Authentic":false,"Price":125,"Year":2021,"jerseyNum":15},{"Player":"Will Smith","Team":"Bel-Air","Sport":"Basketball","Authentic":false,"Price":135,"Year":2007,"jerseyNum":41},{"Player":"Pete Rose","Team":"Cinncinatti Reds","Sport":"Baseball","Authentic":true,"Price":300,"Year":1975,"jerseyNum":14},{"Player":"Derrick Rose","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":2008,"jerseyNum":1},{"Player":"Unknown","Team":"Miami Giants","Sport":"Baseball","Authentic":false,"Price":70,"Year":1950,"jerseyNum":8}];
    
            let isReplica = [];
            for(let i = 0; i < Jerseys.length;i++){
                if(!Jerseys[i].Authentic){
                isReplica.push(Jerseys[i])
                }};
                console.log(isReplica);
    
    Login or Signup to reply.
  2. Use break to stop the loop and push only once:

    const Jerseys = [{"Player":"Shaq","Team":"Miami Heat","Sport":"Basketball","Authentic":true,"Price":325,"Year":2007,"jerseyNum":32},{"Player":"Michael Jordan","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":1992,"jerseyNum":23},{"Player":"Ken Griffey","Team":"Seattle Mariners","Sport":"Baseball","Authentic":true,"Price":300,"Year":1989,"jerseyNum":24},{"Player":"Tim Hardaway","Team":"Miami Heat","Sport":"Basketball","Authentic":false,"Price":135,"Year":1996,"jerseyNum":10},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1994,"jerseyNum":13},{"Player":"Dan Marino","Team":"Miami Dolphins","Sport":"Football","Authentic":true,"Price":300,"Year":1990,"jerseyNum":13},{"Player":"Tim Anderson","Team":"Miami Marlins","Sport":"Baseball","Authentic":false,"Price":125,"Year":2021,"jerseyNum":15},{"Player":"Will Smith","Team":"Bel-Air","Sport":"Basketball","Authentic":false,"Price":135,"Year":2007,"jerseyNum":41},{"Player":"Pete Rose","Team":"Cinncinatti Reds","Sport":"Baseball","Authentic":true,"Price":300,"Year":1975,"jerseyNum":14},{"Player":"Derrick Rose","Team":"Chicago Bulls","Sport":"Basketball","Authentic":true,"Price":300,"Year":2008,"jerseyNum":1},{"Player":"Unknown","Team":"Miami Giants","Sport":"Baseball","Authentic":false,"Price":70,"Year":1950,"jerseyNum":8}];
    
    let isReplica = [];
    for(let i = 0; i < Jerseys.length;i++){
        if(Jerseys[i].Authentic === false){
            isReplica.push(Jerseys[i]);
            break;
        }
    }
    console.log(isReplica);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search