Home > database >  How get item from json in array
How get item from json in array

Time:08-21

[{
        "slide": 1,
        "id": test-1,
    },
    {
        "slide": 2,
        "id": test-2,
    },
]

How can i get only "slide" every time in forEach ? desire solution in console: 1 2

            .then(data => {
                data.forEach(itemElem => {
                    var newElement = jsonElements[slide]);
                    console.log(newElement);
                });
            });

Important, i don't want to use name "slide". Name will change over time. Code need to be flexible

script should take json file made by someone with some data. and then create empty divs with id or some data-atrribute. Next put data inner div if in view using observer API

CodePudding user response:

If data is that json from above

.then(data => {
    data.forEach(itemElem => {
        console.log(itemElem.slide);
    });
});

If you want to log 1 2 as one log

.then(data => {
    console.log(data.map(e => e.slide).join(' '));
});

CodePudding user response:

This will get you the first entry that doesn't have the key "id"

const data = [{
    "slide": 1,
    "id": "test-1",
  },
  {
    "slide": 2,
    "id": "test-2",
  },
];

data.forEach(itemElem => {
  const newElement = itemElem[Object.keys(itemElem).find((name) => name !== "id")];
  console.log(newElement);
});

CodePudding user response:

At some point you will need to tell it that you want to use the "slide" property. However, you can do it at multiple locations and in different ways.

  1. You could map it before using it. This has the benefit, that you can pass the slides array directly to a function, which does not need to know the property name ("slide") to access the value.
data => {
  const slides = data.map(item => item.slide)
  slides.forEach(slide => console.log(slide)
})
  1. You can use a constant or variable to access the property
// either define it as a constant
const SLIDE_KEY = 'slide'
// or pass it as a parameter to the function
myLogPropertiesFunction(data, 'slide')
// and then use it to access the property
data => data.forEach(item => console.log(item[SLIDE_KEY]))

CodePudding user response:

This will get the value from the first prop, no matter what its name is.

.then(data => {
  cconst printMe = data.map(item => {
    return Object.values(item)[0]
  }).join('');
  console.log(printMe);
});

CodePudding user response:

It sounds like the JSON isn't stable. To validate the structure, then pick out the first integer property in the objects, you can use something like this:


.then( data => {

    if( typeof data === 'object' && Object.prototype.toString.call( data ) === '[object Array]' ) {

        for( const i of data ) {

            if( typeof i === 'object' && i !== null ) {

                for( const ii in i ) {

                    if( typeof ii === 'number' && Number.isInteger( ii ) ) {

                        // do something fabulous here
                    }
                }
            }
        }
    }
} );

CodePudding user response:

For your example it would look like this:

.then(data => {
    .forEach(itemElem => {
    const key = Object.keys(itemElem).filter(k => k != 'id')[0];
    var newElement = itemElem[key];
        console.log(newElement);
    });
});
  • Related