简体   繁体   中英

How can I store value in the array on the JavaScript?

My JavaScript code is:

<script type="text/javascript">
    var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"};
    var photoList = [
        {"id":1, "name":"chelsea.jpg"},
        {"id":2, "name":"mu.jpg"},
        {"id":3, "name":"arsenal.jpg"},
        {"id":4, "name":"city.jpg"},
        {"id":5, "name":"liverpool.jpg"}
    ];
    for (var i = 0; i < photoList.length; i++) { 
        if(photoList[i].name == photo.cover1 || photoList[i].name == photo.cover2) {
            console.log(photoList[i].id)
        }
    }
    // I want to store it in array like this = [3,5]
</script>

The result of console.log(photoList[i].id) is 3 and 5.

I want to store it in the an array like this : [3,5] . So if it meets the if condition, then the value is stored in the an array

How can I do it?

You can create an array and push in it.Like

<script type="text/javascript">
    var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"};
    var photoList = [
        {"id":1, "name":"chelsea.jpg"},
        {"id":2, "name":"mu.jpg"},
        {"id":3, "name":"arsenal.jpg"},
        {"id":4, "name":"city.jpg"},
        {"id":5, "name":"liverpool.jpg"}
    ];
    var result = []
    for (var i = 0; i < photoList.length; i++) { 
        if(photoList[i].name == photo.cover1 || photoList[i].name == photo.cover2) {
            result.push(photoList[i].id);
        }
    }
    // I want to store it in array like this = [3,5]
</script>

You could make an array of the cover values and filter photoList and get only the id value of the objects.

 var photo = { cover1: "arsenal.jpg", cover2: "liverpool.jpg" }, photoList = [{ id: 1, name: "chelsea.jpg" }, { id: 2, name: "mu.jpg" }, { id: 3, name: "arsenal.jpg" }, { id: 4, name: "city.jpg" }, { id: 5, name: "liverpool.jpg"}], covers = Object.values(photo), result = photoList .filter(({ name }) => covers.includes(name)) .map(({ id }) => id); console.log(result); 

You could use reduce method and inside check values of other object using some method.

 var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"}; var photoList = [{"id":1, "name":"chelsea.jpg"},{"id":2, "name":"mu.jpg"},{"id":3, "name":"arsenal.jpg"},{"id":4, "name":"city.jpg"},{"id":5, "name":"liverpool.jpg"}]; const result = photoList.reduce((r, {id, name}) => { const match = Object.values(photo).some(e => e == name) if(match) r.push(id); return r; }, []) console.log(result) 

Try the following with Array 's map() and filter() :

 var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"}; var photoList = [ {"id":1, "name":"chelsea.jpg"}, {"id":2, "name":"mu.jpg"}, {"id":3, "name":"arsenal.jpg"}, {"id":4, "name":"city.jpg"}, {"id":5, "name":"liverpool.jpg"} ]; var res = photoList.map(function(p){ if(p.name === photo.cover1 || p.name === photo.cover2) return p.id; }).filter(x => x); console.log(res); 

You can create the array before the loop, and then, from the if block, push the values you want to it.

Your code should look like this:

<script type="text/javascript">
var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"};
var photoList = [
    {"id":1, "name":"chelsea.jpg"},
    {"id":2, "name":"mu.jpg"},
    {"id":3, "name":"arsenal.jpg"},
    {"id":4, "name":"city.jpg"},
    {"id":5, "name":"liverpool.jpg"}
];

var result = [];
for (var i = 0; i < photoList.length; i++) { 
    if(photoList[i].name == photo.cover1 || photoList[i].name == photo.cover2) {
        console.log(photoList[i].id);
        result.push(photoList[i].id);
    }
}
// I want to store it in array like this = [3,5]
</script>

use filter from array

var newarray = photoList.filter( (currentphoto) => {
    return currentphoto.name === photo.cover1 || currentphoto.name === photo.cover2;
})

that's enough

You the reduce method more more detail http://underscorejs.org/#reduce . And use some method.

var photo = {"cover1":"arsenal.jpg", "cover2":"liverpool.jpg"};
var photoList = [{"id":1, "name":"chelsea.jpg"},{"id":2, "name":"mu.jpg"},{"id":3, "name":"arsenal.jpg"},{"id":4, "name":"city.jpg"},{"id":5, "name":"liverpool.jpg"}];

const result = photoList.reduce((r, {id, name}) => {
  const match = Object.values(photo).some(e => e == name)
  if(match) r.push(id);
  return r;
}, [])

console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM