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

101
Views
How can I add a new key with a value to an object in TypeScript?

I have a function whose task is to count the price of a product after a discount. However as a result I get objects without this value (totalPrice). Where am I making mistake?

const arrayObj = [
    {
        basePrice: 12,
        discount: 3,
    },
    {
        basePrice: 12,
        discount: 2,
    },
    {
        basePrice: 8,
        discount: 2,
    },
];

interface Product {
    basePrice: number;
    discount: number;
    totalPrice?: number;
}
const countTotalPrice = (products: Product[]): Product[] => {
    const calculateTotalPrice = (
        basePrice: number,
        discount: number,
        totalPrice?: number
    ): void => {
    
        totalPrice = basePrice - discount;
    };

    products.forEach((product) =>
        calculateTotalPrice(
            product.basePrice,
            product.discount,
            product.totalPrice
        )
    );
    return products;
};
console.log(countTotalPrice(arrayObj));
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are not modifying products in your array. You have two options:

  • use products.forEach and pass in a function that modifies a product. Return modified products.
  • use products.map and pass in a function that takes a product and creates new object with additional property, return the result of mapping.

I would argue that 2nd approach is more idiomatic in JS/TS.

const countTotalPrice = (products: Product[]): Product[] => {
  return products.map((product) => 
    ({    // the parentheses are necessary in this position
          // we want object literal, not code block
          ...product,
          totalPrice: product.basePrice - product.discount
    })
  );
}

Playground link

about 4 years ago · Juan Pablo Isaza Report

0

try this

const countTotalPrice = (products: Product[]): Product[] => {
const calculateTotalPrice = (
 p:Product 
): void => {
    

    p.totalPrice = p.basePrice -p. discount;
};

products.forEach((product) =>
    calculateTotalPrice(
        product
    )
);
return products;
};
about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.prototype.map() combined with Destructuring assignment:

interface Product {
  basePrice: number;
  discount: number;
  totalPrice?: number;
}

const arrayObj: Product[] = [{ basePrice: 12, discount: 3 },{ basePrice: 12, discount: 2 },{ basePrice: 8, discount: 2 },];

const countTotalPrice = (products: Product[]): Product[] =>
  products.map((p) => ({
    ...p,
    totalPrice: p.basePrice - p.discount,
  }));

Code example without TypeScript:

const arrayObj = [{basePrice: 12,discount: 3,},{basePrice: 12,discount: 2,},{basePrice: 8,discount: 2,},]

const countTotalPrice = (products) =>
  products.map((p) => ({
    ...p,
    totalPrice: p.basePrice - p.discount,
  }))

console.log(countTotalPrice(arrayObj))

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!