在JavaScript中合并两个对象并基于单一输入选择多个值

huangapple go评论70阅读模式
英文:

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 &lt;img&gt; element based on an integer input.

CODE:

JS object #1

const sources = {
  1: &quot;assets/images/newyorktimes.png&quot;,
  2: &quot;assets/images/cnn.png&quot;,
  3: &quot;assets/images/bbc.png&quot;,
  4: &quot;assets/images/washingtonpost.png&quot;
};

JS object #2

const sourcenames = {
  1: &quot;New York Times&quot;,
  2: &quot;CNN&quot;,
  3: &quot;BBC&quot;,
  4: &quot;Washington Post&quot;
};

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(&quot;.js-news-content&quot;);
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(&quot;.js-news-image img&quot;);
// 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: &quot;assets/images/newyorktimes.png&quot;,
  2: &quot;assets/images/cnn.png&quot;,
  3: &quot;assets/images/bbc.png&quot;,
  4: &quot;assets/images/washingtonpost.png&quot;
});

const sourcenames = Object.assign(Object.create(null),{
  1: &quot;New York Times&quot;,
  2: &quot;CNN&quot;,
  3: &quot;BBC&quot;,
  4: &quot;Washington Post&quot;
});

// Here we hardcode 
// the case when fictionsource 
// is equal to 1. 
const stimulus = {
  fictionsource: 1,
  content: &quot;&lt;div&gt;test&lt;/div&gt;&quot;
}

const { 
  fictionsource: fictionSrc,  // INPUT HERE (e.g., 1)
  content: contentSrc         // CONTENT HERE
} = stimulus;


const newsImage = node.querySelector(&quot;.js-news-image img&quot;);

if (newsImage &amp;&amp; typeof sources[fictionSrc] !== &#39;undefined&#39; &amp;&amp; typeof sourcenames[fictionSrc] !== undefined) {
   newsImage.setAttribute(&quot;src&quot;, sources[fictionSrc]);
   newsImage.setAttribute(&quot;alt&quot;, sourcenames[fictionSrc]);
} else {
	throw new Error(&quot;Incorrect data&quot;);
}

This is how you can "merge" the objects in one:

const sources = {
  1: &quot;assets/images/newyorktimes.png&quot;,
  2: &quot;assets/images/cnn.png&quot;,
  3: &quot;assets/images/bbc.png&quot;,
  4: &quot;assets/images/washingtonpost.png&quot;
};

const sourcenames = {
  1: &quot;New York Times&quot;,
  2: &quot;CNN&quot;,
  3: &quot;BBC&quot;,
  4: &quot;Washington Post&quot;
};

const result = {}

Object.keys(sources).forEach((x) =&gt; {
	result[sources[x]] = sourcenames[x];
});

console.log(result);

And one idea better:

const sources = {
  1: &quot;assets/images/newyorktimes.png&quot;,
  2: &quot;assets/images/cnn.png&quot;,
  3: &quot;assets/images/bbc.png&quot;,
  4: &quot;assets/images/washingtonpost.png&quot;
};

const sourcenames = {
  1: &quot;New York Times&quot;,
  2: &quot;CNN&quot;,
  3: &quot;BBC&quot;,
  4: &quot;Washington Post&quot;
};

const result = Object.keys(sources).reduce((acc, value) =&gt; {
    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: &quot;assets/images/newyorktimes.png&quot;,
    2: &quot;assets/images/cnn.png&quot;,
    3: &quot;assets/images/bbc.png&quot;,
    4: &quot;assets/images/washingtonpost.png&quot;
});

const sourcenames = Object.assign(Object.create(null),{
    1: &quot;New York Times&quot;,
    2: &quot;CNN&quot;,
    3: &quot;BBC&quot;,
    4: &quot;Washington Post&quot;
});

const result = Object.keys(sources).
filter((key) =&gt; typeof sourcenames[key] !== &#39;undefined&#39;).
reduce((acc, value) =&gt; {
    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 = [&quot;assets/images/newyorktimes.png&quot;, &quot;assets/images/cnn.png&quot;, &quot;assets/images/bbc.png&quot;, &quot;assets/images/washingtonpost.png&quot; ]
newsImage.src = sources[value]

Hope this help !

huangapple
  • 本文由 发表于 2020年1月6日 22:30:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定