Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
how to fetch a value from specific index of object property that has the values of pushed array in javascript

I am trying to get a value from specific index of object property. I have used push function to push the value to object property, but when I call result.marks[0], it returns all the values in an array.

<!DOCTYPE html>
<html>

    <body>
        <p id="demo"></p>
        <script>
        try {
            let result = {
                marks: [], // 
            };
            const n = 5;
            let text = "";
            for (let i = 0; i < n; i++) {
                text += prompt("Enter value of " + i, i) + "<br>";
            }
            result.marks.push(text);
            document.getElementById("demo").innerHTML = result.marks[0]; // it does not print the specific index value.it return the whole values in an array.
        }
        catch (err) {
            document.write(err);
        };
        </script>
    </body>

</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Inside your loop you concatenate all the inputs in one string and push that one string to array.

loop 1: text="0<br>"
loop 2: text="0<br>1<br>"
and so on.

at the end of your loop your text value is "0<br>1<br>2<br>3<br>4<br>5<br>" and then you push it to array

so when you fetch the index 0 element it returns the string with all values. What you can do is stop concatenating and push each input to array inside the loop

<html>

    <body>
        <p id="demo"></p>
        <script>
        try {
            let result = {
                marks: [], // 
            };
            const n = 5;
            let text = "";
            for (let i = 0; i < n; i++) {
                text = prompt("Enter value of " + i, i) + "<br>";
                result.marks.push(text)
            }
            document.getElementById("demo").innerHTML = result.marks[1]; // it now returns value from specific index.
        }
        catch (err) {
            document.write(err);
        };
    </script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda