英文:
Merging two objects in JS and select multiple values based on single input
问题
问题:
我希望将两个对象合并在一起,并根据整数输入选择多个值。更具体地说,我想根据整数输入为<img>
元素分配图像源和alt标题。
代码:
JS对象#1
const sources = {
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
};
JS对象#2
const sourcenames = {
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
};
输入值:
我从参数中获取输入值,我以以下方式解构:
const {
fictionsource: fictionSrc, // 在此输入(例如,1)
content: contentSrc // 在此输入内容
} = stimulus;
我以以下方式分配内容:
const newsContent = node.querySelector(".js-news-content");
newsContent.innerHTML = contentSrc;
现在,我希望根据输入值分配图像源和alt文本。例如,如果值为1,则我希望发生以下情况:
const newsImage = node.querySelector(".js-news-image img");
// 将assets/images/newyorktimes.png分配给src属性
// 将“New York Times”分配给alt属性
期望的输出:
推荐的方法是将两个对象合并,并根据输入值分配图像源和alt文本。
英文:
PROBLEM:
I wish to merge two objects together and select multiple values based on an integer input. More specifically, I want to assign image source and alt-title to a <img>
element based on an integer input.
CODE:
JS object #1
const sources = {
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
};
JS object #2
const sourcenames = {
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
};
INPUT VALUE:
I get the input value from a parameter, which I deconstruct in the following way:
const {
fictionsource: fictionSrc, // INPUT HERE (e.g., 1)
content: contentSrc // CONTENT HERE
} = stimulus;
I assign content in the following way:
const newsContent = node.querySelector(".js-news-content");
newsContent.innerHTML = contentSrc;
Now, I wish to assign image source and alt-text based on the input value. For instance, if the value is 1, then I wish the following to happen:
const newsImage = node.querySelector(".js-news-image img");
// Assign assets/images/newyorktimes.png to src-attribute
// Assign New York Times to alt-attribute
DESIRED OUTPUT:
The recommended approach to combine the two objects and to assign image source and alt-text based on input value.
答案1
得分: 1
以下是您需要的翻译部分:
// In principle is better to have
// empty objects.
const sources = Object.assign(Object.create(null),{
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
});
const sourcenames = Object.assign(Object.create(null),{
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
});
// Here we hardcode
// the case when fictionsource
// is equal to 1.
const stimulus = {
fictionsource: 1,
content: "<div>test</div>"
}
const {
fictionsource: fictionSrc, // INPUT HERE (e.g., 1)
content: contentSrc // CONTENT HERE
} = stimulus;
const newsImage = node.querySelector(".js-news-image img");
if (newsImage && typeof sources[fictionSrc] !== 'undefined' && typeof sourcenames[fictionSrc] !== undefined) {
newsImage.setAttribute("src", sources[fictionSrc]);
newsImage.setAttribute("alt", sourcenames[fictionSrc]);
} else {
throw new Error("Incorrect data");
}
这是您要求的翻译结果。如果您需要其他部分的翻译,请提供具体的文本。
英文:
You need something like this:
// In principle is better to have
// empty objects.
const sources = Object.assign(Object.create(null),{
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
});
const sourcenames = Object.assign(Object.create(null),{
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
});
// Here we hardcode
// the case when fictionsource
// is equal to 1.
const stimulus = {
fictionsource: 1,
content: "<div>test</div>"
}
const {
fictionsource: fictionSrc, // INPUT HERE (e.g., 1)
content: contentSrc // CONTENT HERE
} = stimulus;
const newsImage = node.querySelector(".js-news-image img");
if (newsImage && typeof sources[fictionSrc] !== 'undefined' && typeof sourcenames[fictionSrc] !== undefined) {
newsImage.setAttribute("src", sources[fictionSrc]);
newsImage.setAttribute("alt", sourcenames[fictionSrc]);
} else {
throw new Error("Incorrect data");
}
This is how you can "merge" the objects in one:
const sources = {
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
};
const sourcenames = {
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
};
const result = {}
Object.keys(sources).forEach((x) => {
result[sources[x]] = sourcenames[x];
});
console.log(result);
And one idea better:
const sources = {
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
};
const sourcenames = {
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
};
const result = Object.keys(sources).reduce((acc, value) => {
acc[sources[value]] = sourcenames[value];
return acc;
},Object.create(null));
console.log(result);
In case you want to keep the indexes and have nested objects check this example:
const sources = Object.assign(Object.create(null), {
1: "assets/images/newyorktimes.png",
2: "assets/images/cnn.png",
3: "assets/images/bbc.png",
4: "assets/images/washingtonpost.png"
});
const sourcenames = Object.assign(Object.create(null),{
1: "New York Times",
2: "CNN",
3: "BBC",
4: "Washington Post"
});
const result = Object.keys(sources).
filter((key) => typeof sourcenames[key] !== 'undefined').
reduce((acc, value) => {
acc[value] = Object.assign(Object.create(null), {
source: sources[value],
sourcename: sourcenames[value]
});
return acc;
},Object.create(null));
console.log(result);
Edit: If you use array instead object the cons is in a bit slow access
to elements. You will need to search in the array for according element. This is in case if you don't have array indexes.
答案2
得分: 0
你可以像这样循环遍历你的对象:
let source = null
for(var key in sources){
if ( value == key ) // value is the number you are looking for
sources = sources.key
}
newsImage.src = source
但如果你的对象是数组,那会更简单,你可以直接访问它:
sources = ["assets/images/newyorktimes.png", "assets/images/cnn.png", "assets/images/bbc.png", "assets/images/washingtonpost.png"]
newsImage.src = sources[value]
希望这有帮助!
英文:
You could loop through your objects like this :
let source = null
for(var key in sources){
if ( value == key ) // value is the number you are looking for
sources = sources.key
}
newsImage.src = source
But it would be easyer if your objects were array, then you could access it directly :
sources = ["assets/images/newyorktimes.png", "assets/images/cnn.png", "assets/images/bbc.png", "assets/images/washingtonpost.png" ]
newsImage.src = sources[value]
Hope this help !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论