-3

can any one help in this i am trying to compare two different arrays for pushing values when comparision is equal. below are my two(imageslide.therapy),totalValues arrays and i want compare names like cats and dogs. if condition is true then i need to push their images urls.

var imageslide = {
"therapy": [
    {
        "name": "cats",
        "images": [
            { "url": "cat/firstimg.jpg" },
            { "url": "cat/secondimg.jpg" },
            { "url": "cat/thirdimg.jpg" },
            { "url": "cat/fourthimg.jpg" }
        ]
    },

    {
        "name": "dogs",
        "images": [
            { "url": "dog/firstdog.jpeg" },
            { "url": "dog/seconddog.jpg" },
            { "url": "dog/thirddog.jpg" },
            { "url": "dog/fourthdog.jpg" }
        ]
    },

  ]
}

var totalValues = ["cats","dogs"];

and i tried like below

var imageArray = imageslide.therapy
function compare(imageArray,totalValues ){
    imageArray.forEach((e1)=>totalValues.forEach((e2)=>{

        if(e1.name==e2){
    console.log(e1.name,",",e2)
        }
    })
Mark
  • 90,562
  • 7
  • 108
  • 148
  • I can't understand what you are trying to do. It might help if you can show what you are starting with and what you want as a final result. – Mark Aug 09 '18 at 04:48
  • i want to compare two arrays . if cats == cats then i need to push cats image urls into a new array. – deepak tinku Aug 09 '18 at 04:53
  • Your English is fine. It's the code I'm having trouble understanding. You `forEach` statements work (one you close the `)` and `}` and call the function. It outputs `cats , cats \n dogs , dogs`. It would just help if you showed the expected output. – Mark Aug 09 '18 at 05:06
  • Possible duplicate of [Compare two arrays and push different values to new array](https://stackoverflow.com/questions/29320678/compare-two-arrays-and-push-different-values-to-new-array) – Abhishek Kumar Aug 09 '18 at 05:07
  • thank you mark meyer and abhishek for your response . i solved that. – deepak tinku Aug 09 '18 at 07:21

5 Answers5

1

For what I understand from your question here is the answer. Please forgive me I don't know much about arrow function so I wrote it in simple javascript.

var imageslide = {
    "therapy": [
                    {
                        "name": "cats",
                        "images": [
                            { "url": "cat/firstimg.jpg" },
                            { "url": "cat/secondimg.jpg" },
                            { "url": "cat/thirdimg.jpg" },
                            { "url": "cat/fourthimg.jpg" }
                        ]
                    },

                    {
                        "name": "dogs",
                        "images": [
                            { "url": "dog/firstdog.jpeg" },
                            { "url": "dog/seconddog.jpg" },
                            { "url": "dog/thirddog.jpg" },
                            { "url": "dog/fourthdog.jpg" }
                        ]
                    },

                ]
}
var totalValues = ["cats","dogs"];
var imageArray = imageslide.therapy
function compare(imageArray,totalValues ){
    for(var i=0;i<imageArray.length;i++){
        for(var j=0;j<totalValues.length;j++){
            if(totalValues[j]=imageArray[i].name){
                console.log(imageArray[i].name+"=="+totalValues[j]);
                //imageArray[i].images.push({"url": "https://hexasoft.io"});
                //break;
                return imageArray[i].images;
            }
        }

    }
    //printResult(imageArray);
    return [];
}
function printResult(resultArray){
    for(var i=0;i<resultArray.length;i++) {
        console.log(resultArray[i].name);
        for(var j=0;j<resultArray[i].images.length;j++){
            console.log(resultArray[i].images[j]);
        }
    }
}
images = compare(imageArray, totalValues);
if(images.length > 0){
     for(var i=0;i<images.length; i++){
         images[i].push({"url": "your url"});
     }  
}
root
  • 518
  • 4
  • 20
  • You can pass the url to function to be pushed into array or you can simply return the images of the matching results and push the url into array. – root Aug 09 '18 at 05:32
  • i tried to vote up root but i am not able t do that . because of less reputations. once i get those then i will vote up definately. – deepak tinku Aug 09 '18 at 10:04
0

Check out the javascript filter function (Link for the docs).

In your case, you want to do something like this:

function getImagesByAnimalName(animal_name){
  var imageArray = imageslide.therapy;
  
  var animalImages = imageArray.filter(animalData => {
    return animalData.name === animal_name;
  })
  return animalImages[0].images;
}
0

Try it like this. The function will return URLs for each element in totalValues array.

var totalValues = ["cats"];
var slides = imageslide.therapy;

function comp(slides, totalValues ){
  let retVal;
  for( val of totalValues ) {
    for( thisTh of slides ) {
      if( thisTh.name == val ){
        retVal = thisTh.images;
      }
    }
  }
  
  return retVal;
}
Simranjit Singh
  • 384
  • 1
  • 6
0

The following will create pics, a flat array of image URLs, if this is what you want:

var pics=[].concat(...imageslide.therapy.map(el=>{
  if (totalValues.indexOf(el.name)>-1) 
return el.images.map(e=>e.url)}))
console.log(pics);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0
function compare(imageArray, totalValues) {
    for (var a = 0; a < imageArray.length; a++) {
        for (var j = 0; j < totalValues.length; j++) {
            if (totalValues[j] == imageArray[a].name) {
                allValues.push(imageArray[a].images);
                for (var i = 0; i < allValues.length; i++) {
                   for(var j = 0; j < allValues[i].length; j++){
                    buildSlide(allValues[i][j].url);
                }
                }
            }
        }
    }
    displaySlides(slide_index);
}