如何将 key/value 对添加到另一个对象中的 JavaScript 对象
How can I add a key/value pair to a JavaScript object which is in an another object
我请求您提供以下代码的帮助:
function vimeoImport() {
let videosToBeImported = [{
uri: "/videos/442638455",
name: "FOMME_1387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
},{
uri: "/videos/442056086",
name: "FOMME_1387_VD2",
modifed_time: "2020-07-29T09:25:27+00:00"
},{
uri: "/videos/442638455",
name: "FOMME_2387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
}];
let frtVideoUrlValues = {};
for (var index in videosToBeImported) {
var videos = videosToBeImported[index];
let videoName = videos.name;
let splitName = videoName.split('_');
let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
let positionvideo = splitName[2];
let variationGroupParams = {};
variationGroupParams[positionvideo] = videos.uri;
if (targetedVariationGroup in frtVideoUrlValues) {
frtVideoUrlValues[targetedVariationGroup] += variationGroupParams;
} else {
frtVideoUrlValues[targetedVariationGroup] = variationGroupParams;
}
}
}
我试图在 frtVideoUrlValues 对象中的 targetedVariationGroup 对象中添加一个 key/value 对(键是一个变量)。当我尝试时,我在 targetedVariationGroup 中看到了新的 key/value 对,但合并不起作用并且无法访问 2 个对象:
然后我尝试获取这样的对象:
由于您没有提供任何输出格式,我猜输出应该如下所示(如果这不是您想要的,请提供正确的输出格式):
{
FOMME_1387: [
{ VD1: '/videos/442638455' },
{ VD2: '/videos/442056086' }
],
FOMME_2387: [
{ VD1: '/videos/442638455' }
]
}
现在,要实现这一点,您应该编写如下代码:
function vimeoImport() {
let videosToBeImported = [
{
uri: "/videos/442638455",
name: "FOMME_1387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
},
{
uri: "/videos/442056086",
name: "FOMME_1387_VD2",
modifed_time: "2020-07-29T09:25:27+00:00"
},
{
uri: "/videos/442638455",
name: "FOMME_2387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
}
];
let frtVideoUrlValues = {};
for (var index in videosToBeImported) {
var videos = videosToBeImported[index];
let videoName = videos.name;
let splitName = videoName.split('_');
let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
let positionvideo = splitName[2];
let variationGroupParams = {};
variationGroupParams[positionvideo] = videos.uri;
// here are the changes I've made
if(frtVideoUrlValues[targetedVariationGroup] === undefined) {
frtVideoUrlValues[targetedVariationGroup] = [];
}
frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);
}
console.log(frtVideoUrlValues);
}
vimeoImport();
您的代码存在问题,您使用 + 将对象与另一个对象相加,但 + 仅用于连接 javascript 中的字符串。您应该将对象推入数组,而不是您正在做的事情。要在数组中添加新元素,您必须使用 push() 方法。
此外,请注意,如果 frtVideoUrlValues 中的键 targetedVariationGroup 是 undefined,我已经将 空数组 分配给 targetedVariationGroup,如下所示:
frtVideoUrlValues[targetedVariationGroup] = [];
然后,将 variationGroupParams 对象推送到数组中,如下所示:
frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);
我请求您提供以下代码的帮助:
function vimeoImport() {
let videosToBeImported = [{
uri: "/videos/442638455",
name: "FOMME_1387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
},{
uri: "/videos/442056086",
name: "FOMME_1387_VD2",
modifed_time: "2020-07-29T09:25:27+00:00"
},{
uri: "/videos/442638455",
name: "FOMME_2387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
}];
let frtVideoUrlValues = {};
for (var index in videosToBeImported) {
var videos = videosToBeImported[index];
let videoName = videos.name;
let splitName = videoName.split('_');
let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
let positionvideo = splitName[2];
let variationGroupParams = {};
variationGroupParams[positionvideo] = videos.uri;
if (targetedVariationGroup in frtVideoUrlValues) {
frtVideoUrlValues[targetedVariationGroup] += variationGroupParams;
} else {
frtVideoUrlValues[targetedVariationGroup] = variationGroupParams;
}
}
}
我试图在 frtVideoUrlValues 对象中的 targetedVariationGroup 对象中添加一个 key/value 对(键是一个变量)。当我尝试时,我在 targetedVariationGroup 中看到了新的 key/value 对,但合并不起作用并且无法访问 2 个对象:
然后我尝试获取这样的对象:
由于您没有提供任何输出格式,我猜输出应该如下所示(如果这不是您想要的,请提供正确的输出格式):
{
FOMME_1387: [
{ VD1: '/videos/442638455' },
{ VD2: '/videos/442056086' }
],
FOMME_2387: [
{ VD1: '/videos/442638455' }
]
}
现在,要实现这一点,您应该编写如下代码:
function vimeoImport() {
let videosToBeImported = [
{
uri: "/videos/442638455",
name: "FOMME_1387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
},
{
uri: "/videos/442056086",
name: "FOMME_1387_VD2",
modifed_time: "2020-07-29T09:25:27+00:00"
},
{
uri: "/videos/442638455",
name: "FOMME_2387_VD1",
modifed_time: "2020-07-29T09:24:48+00:00"
}
];
let frtVideoUrlValues = {};
for (var index in videosToBeImported) {
var videos = videosToBeImported[index];
let videoName = videos.name;
let splitName = videoName.split('_');
let targetedVariationGroup = splitName[0].concat('_', splitName[1]);
let positionvideo = splitName[2];
let variationGroupParams = {};
variationGroupParams[positionvideo] = videos.uri;
// here are the changes I've made
if(frtVideoUrlValues[targetedVariationGroup] === undefined) {
frtVideoUrlValues[targetedVariationGroup] = [];
}
frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);
}
console.log(frtVideoUrlValues);
}
vimeoImport();
您的代码存在问题,您使用 + 将对象与另一个对象相加,但 + 仅用于连接 javascript 中的字符串。您应该将对象推入数组,而不是您正在做的事情。要在数组中添加新元素,您必须使用 push() 方法。
此外,请注意,如果 frtVideoUrlValues 中的键 targetedVariationGroup 是 undefined,我已经将 空数组 分配给 targetedVariationGroup,如下所示:
frtVideoUrlValues[targetedVariationGroup] = [];
然后,将 variationGroupParams 对象推送到数组中,如下所示:
frtVideoUrlValues[targetedVariationGroup].push(variationGroupParams);