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

167
Views
How I can get an index value of a array inside a another array

I have a array in this format

const Adicionais = [{
  ID: 0,
  Nome: 'Adiocinais Açai',
  MaxOpcoes: 3,
  Itens: [{
    Nome: 'Morango',
    Valor: 5.00,
    ID: 0,
  }, {
    Nome: 'Raspas de Chocolate',
    Valor: 5.00,
    ID: 1,
  }, {
    Nome: 'Biz de Chocolate',
    Valor: 5.00,
    ID: 2,
  }]
}]

I need to get the value of index VALOR inside the array ITENS and SUM the value of all items that I have an ID saved in another array of the items selected. I tried do this that form

const ValorAdicionas = ProdutoAdicionais.filter((item) =>  (item.Itens.filter((a) => (adicionaisChecked.includes(a.ID))).reduce((a, v) => a = a + v.Valor, 0)))

but does not work. The array ProdutosAdicionais have the value of array Adicionais, and the array adicionaisChecked have the ID of array Itens

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I hope this code helping you

const Adicionais = [{
  ID: 0,
  Nome: 'Adiocinais Açai',
  MaxOpcoes: 3,
  Itens: [{
    Nome: 'Morango',
    Valor: 5.00,
    ID: 0,
  }, {
    Nome: 'Raspas de Chocolate',
    Valor: 5.00,
    ID: 1,
  }, {
    Nome: 'Biz de Chocolate',
    Valor: 5.00,
    ID: 2,
  }]
}]
adicionaisChecked = [0,1]
Adicionais.map(o=>o.Itens.filter(v=>adicionaisChecked.indexOf(v.ID) != -1)).map(k=>k.reduce((sum, prev)=>sum + prev.Valor,0))
about 4 years ago · Juan Pablo Isaza Report

0

const adicionaisChecked=[1];
const ProdutoAdicionais = [{
  ID: 0,
  Nome: 'Adiocinais Açai',
  MaxOpcoes: 3,
  Itens: [{
    Nome: 'Morango',
    Valor: 5.00,
    ID: 0,
  }, {
    Nome: 'Raspas de Chocolate',
    Valor: 5.00,
    ID: 1,
  }, {
    Nome: 'Biz de Chocolate',
    Valor: 5.00,
    ID: 2,
  }]
}]


const valorAdicionas = ProdutoAdicionais.reduce((acc,item)=>{
 return acc.concat(item.Itens.filter(a=>adicionaisChecked.includes(a.ID)))
},[]).reduce((a,v)=>a+v.Valor,0);

console.log(valorAdicionas);

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!