1

I'm trying to append the object from the array. I try to loop through the array but it only appends one element of the array to the object. fiddle: https://jsfiddle.net/maherafrasiab/9ehkfvoc/3/

Edited:

    var shers = {};
// the .pbgmain will return the following array.
    var all = [{sher: "some text"},{sher: "some text"},{sher: "some text"}];
//basically i want to convert the above array into object.
    var data = $('.pbgmain').each(function(i) {
        datas = $(this).text().trim(); 
        all.push({sher: datas})  
        for(var i = 0; i < 10; i++) {
            shers = all[i];
        }
    });
11
  • 1
    You keep overwriting shers. What did you intend shers to be? What would be its properties? Commented Jan 22, 2021 at 8:20
  • @trincot these are collection couplets of poems in the Urdu language. Its properties would be every single couplet which sher. Commented Jan 22, 2021 at 9:40
  • Why are you iterating exactly 10 times? What if there's less than 10 items? Commented Jan 22, 2021 at 9:40
  • It isn't meaningful to "append values" to an object. An object only contains keyed properties (i.e. named properties). I think you're misunderstanding how JavaScript's object and array types work. Commented Jan 22, 2021 at 9:41
  • @Dai the array contains 10 elements. that is what I'm iteratinng Commented Jan 22, 2021 at 9:41

1 Answer 1

1

the .pbgmain will return the following array.

var all = [{sher: "some text"},{sher: "some text"},{sher: "some text"}];

You just need this (and you don't need to use jQuery!):

const elements         = document.querySelectorAll( '.pbgmain' );
const elementArray     = Array.from( elements );
const elementTexts     = elementArray.map( e => e.textContent.trim() );
const asArrayOfObjects = elementTexts.map( text => ( { sher: text } ) );
console.log( asArrayOfObjects );

This can be shortened to this if you want to be more succint:

const asArrayOfObjects = Array.from( document.querySelectorAll( '.pbgmain' ) ).map( e => ( { sher: e.textContent.trim() } );
console.log( asArrayOfObjects );
Sign up to request clarification or add additional context in comments.

7 Comments

It is giving me cannot read property of undefined.
but the problem is I don't want the array of objects. I want the single object.
@MaherAfrasiab I can't really help you further unless you create a JSFiddle or Snippet with the original HTML, as well as example JSON that shows the kind of output you want.
@MaherAfrasiab The example you gave as "{sher: "some text to be selected"},{sher: "some text to be selected"},{sher: "some text to be selected"}" is invalid. You cannot use the same name (key) for different properties: you can either have an array of strings, an array of separate objects (where each object has the sher property), or a single object with different property names.
|

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.