1

Hi I am having an array like below and trying to push the item on map, but i am not able to push, Please tell what wrong i am doing here.

firstClassPrices: [
 [
 "Kan ej ombokas",
 "431",
 "SEK",
 "Företagspris"
 ],
 [
 "Kan ombokas",
 "525",
 "SEK",
 "Företagspris"
 ]
]

trying to push '1' like below:

 let ticketi = firstClassPrices.map((price) => {
 price.push("1");
 });

i need o/p as

firstClassPrices: [
 [
 "Kan ej ombokas",
 "431",
 "SEK",
 "Företagspris",
 "1"
 ],
 [
 "Kan ombokas",
 "525",
 "SEK",
 "Företagspris",
 "1"
 ]
 ]
asked Jun 23, 2022 at 8:41
2
  • 1
    Your code already works. You're only using .map() while not returning any elements from its callback function so the return value (which you store in ticketi will only contain undefined values. But the original firstClassPrices will contain what you are looking for. Commented Jun 23, 2022 at 8:47
  • if "1" is already there in array how to skip the push Commented Jun 23, 2022 at 11:06

4 Answers 4

3

You have to return the price

const firstClassPrices = [
 [
 "Kan ej ombokas",
 "431",
 "SEK",
 "Företagspris"
 ],
 [
 "Kan ombokas",
 "525",
 "SEK",
 "Företagspris"
 ]
];
 let ticketi = firstClassPrices.map((price) => {
 // skip if price already includes 1
 if (!price.includes("1")) {
 price.push("1");
 }
 return price;
 });
console.log(ticketi);

answered Jun 23, 2022 at 8:46
Sign up to request clarification or add additional context in comments.

3 Comments

In this code, both firstClassPrices and ticketi reference the same nested arrays. There is little added value in using .map() if you are going to modify the original array also.
if "1" is already there in array how to skip the push
@Sudhir I updated the code with this requirement.
2

Your approach mutates the old array and maps undefined.


Instead, you could map the old and new content of the array.

This approach does not mutate the old data.

let ticketi = firstClassPrices.map(price => [...price, '1']); 
answered Jun 23, 2022 at 8:47

1 Comment

Notice that according to OP: 1 should be of type string
1

You can use Array.prototype.map() combined with Array.prototype.concat()

Code:

const firstClassPrices = [["Kan ej ombokas","431","SEK","Företagspris"],["Kan ombokas","525","SEK","Företagspris"]]
const ticketi = firstClassPrices.map(p => p.concat('1'))
console.log(ticketi)

answered Jun 23, 2022 at 9:01

Comments

1

Since you want to mutate the original arrays, you can use the function Array.prototype.forEach.

The problem you're facing is regarding the missing return value inside of the map's handler, so basically, the handler is returning undefined

const array = [ [ "Kan ej ombokas", "431", "SEK", "Företagspris" ], [ "Kan ombokas", "525", "SEK", "Företagspris" ]],
 handler = (a) => a.push("1");
array.forEach(handler);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Jun 23, 2022 at 9:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.