0

I have situation about converting number to string. In the function below. I am iterating the objects and trying to change value type of plan_price. it's working when I do console.log()

    bestPlanArrange(bridals){
        let plans = [];
        bridals.filter(item => {
            item.plans.filter(plan => {
                plans.push(plan);
            });
        })
        let obj = {}
        let planArr = [];
        plans.filter(item => {
            item.plan_price.toString()
            console.log(item.plan_price) // doesn't listen the code above not working. 
            console.log(item.plan_price.toString()) // it's working like this.
            planArr.push(item) // I want to push after covert.
        })
        if (planArr[0] != null && plans[0].plan_price != null) {
            obj = planArr[0];
        }
        return obj;
    },

Do I missing something or doing something wrong here?

5
  • 2
    Why are you calling filter when the function doesn't return a true/false value and you're not using the result? It seems like you're using it when you really mean forEach. Commented Sep 19, 2019 at 1:41
  • Please provide an minimal reproducible example. This is not easy to test without sample input and a runnable snippet. Commented Sep 19, 2019 at 1:41
  • 2
    And if you're using it to create a new array with values for each element of the original array, you should use .map(). Commented Sep 19, 2019 at 1:43
  • let planArr = plans.map(item => item.plan_price.toString()) Commented Sep 19, 2019 at 1:43
  • thank you for pointing those out. @Barmar Commented Sep 19, 2019 at 1:44

1 Answer 1

4

Calling toString() doesn't change the value in place. You have to assign the result back to a variable:

item.plan_price = item.plan_price.toString()
Sign up to request clarification or add additional context in comments.

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.