0

I would like to know how can I get a value from an array of array using map javascript function.

My response from a query :

[ 
    { 
        customer_id: '5d0757aa4b6620003335aff2',
        cars: [ 
            {
                type: 'break', // unique
                serial_number: '0000X523654FE'
                // ... other data, not useful here
            },
            {
                type: 'sport',
                serial_number: '485605FEL45E'
                // ...
            }   
        ]
    }
]

I want to get serial_number string, so here what I did with map javascript function :

const serialNumber = response.items.map(element => {

                    return element.cars.map(car => {
                        if (car.type == "sport") {
                            return car.serial_number;
                        }
                    });
                });

And I get :

[ [ '485605FEL45E' ] ]

I would like to get : '485605FEL45E'

How can remove the double array ? I just want the value.

Thank you very much !

EDIT : I didn't specify something important, type is unique, I can't get two sport cars for the same customer.

8
  • 1
    serialNumber[0][0]???? Commented Aug 9, 2019 at 9:43
  • 1
    you might be after .flat() or .flatMap() if you want to remove the inner array? Commented Aug 9, 2019 at 9:44
  • 2
    If you're removing items from the array, you're going to want to use filter too. Commented Aug 9, 2019 at 9:44
  • 1
    you should be using .find on cars array not map. Commented Aug 9, 2019 at 9:46
  • 1
    Looking at your want -. '485605FEL45E', this implies your only want 1 single results.. Is this correct, and if so is it just the first it finds.? Multiple results would look like -> ['abc', '123'],... Commented Aug 9, 2019 at 9:54

5 Answers 5

2

You can use find instead

let data = [{
  customer_id: '5d0757aa4b6620003335aff2',
  cars: [{
      type: 'break',
      serial_number: '0000X523654FE'
    },
    {
      type: 'sport',
      serial_number: '485605FEL45E'
    }
  ]
}]

const serialNumber = data.map(element => {
  let found = element.cars.find(car => {
    if (car.type == "sport") {
      return car.serial_number;
    }
  });
  return (found || {}).serial_number
});

console.log(serialNumber)

If there are multiple entries with 'sport' you can use filter instead of find

let data = [{
  customer_id: '5d0757aa4b6620003335aff2',
  cars: [{
      type: 'break',
      serial_number: '0000X523654FE'
    },
    {
      type: 'sport',
      serial_number: '485605FEL45E'
    },
    {
      type: 'sport',
      serial_number: '657445FEL45E'
    }
  ]
}]

const serialNumber = data.map(element => {
  let found = element.cars.reduce((op, car) => {
    if (car.type == "sport") {
      op.push(car.serial_number)
    }
    return op
  }, []);
  return found.length && found
});

console.log(serialNumber.flat())

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

10 Comments

Maybe because OP asked for using map function.
Not me, but what I would say is that remember find will only return 1 item. What if there are multiple cars that are of type sport.
not instead, but should be using find don't understand a downvote.
@Keith OP wants to flatten the array so surely there's only one element.
@AZ_ Not sure why flattening the array should stop the result having multiple serial numbers for multiple sports cars. IOW: If the user is after all sport cars serial number, find will not work. Or do you mean because the OP has put wants -> '485605FEL45E' might need to clarify with OP if he really only wants a single result.
|
1

You can just use Array.join("").

let items = [{
  customer_id: '5d0757aa4b6620003335aff2',
  cars: [{
      type: 'break',
      serial_number: '0000X523654FE'
    },
    {
      type: 'sport',
      serial_number: '485605FEL45E'
    }
  ]
}]

const serialNumber = items.map(element => {

                return element.cars.map(car => {
                    if (car.type == "sport") {
                        return car.serial_number;
                    }
                });
            });

console.log(serialNumber.join(""));

1 Comment

I accept this answer because with the join function I get exactly what I want : 'value'. Thank you very much
0

Try using find()

let no;
[
    {
        customer_id: '5d0757aa4b6620003335aff2',
        cars: [
            {
                type: 'break',
                serial_number: '0000X523654FE'
            },
            {
                type: 'sport',
                serial_number: '485605FEL45E'
            }
        ]
    }
]
.find(element => { return element.cars.find(i => !!(i.type === 'sport' && (no = i.serial_number))) })

console.log(no)

Comments

0

You can use Array.find() like this:

const response = [{
  customer_id: '5d0757aa4b6620003335aff2',
  cars: [{
      type: 'break',
      serial_number: '0000X523654FE'
      // ... other data, not useful here
    },
    {
      type: 'sport',
      serial_number: '485605FEL45E'
      // ...
    }
  ]
}]

let serialNumber = null;

response.find(element => {
  return element.cars.find(car => {
    if (car.type == "sport") {
      serialNumber = car.serial_number;
      return true;
    }
  });
});

console.log(serialNumber);

Comments

0

Use Array.prototype.find on cars array as following and return the serialNumber to outer map

let data = [{
	customer_id: '5d0757aa4b6620003335aff2',
	cars: [{
		type: 'break',
		serial_number: '0000X523654FE'
	},
	{
		type: 'sport',
		serial_number: '485605FEL45E'
	}
	]
},
{
	customer_id: '5d0757aa4b6620003335aff2',
	cars: [{
		type: 'break',
		serial_number: '0000X523654FE'
	},
	{
		type: 'sport',
		serial_number: '485605FEL45E'
	}
	]
}]


let out = data.map(({cars}) => cars.find(e => e.type === 'sport').serial_number);
console.log(out)

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.