对象内数组中的外部数组元素

时间:2021-12-07 15:53:51

I have an array of skills like so:

我有一系列的技能:

var a_skill = ['problem solving', 'collaboration', 'public speaking'];

I also have an object array like so:

我也有一个像这样的对象数组:

var o_job[0] = {title: "developer", skills:[skill[0], skill[2]]};
var o_job[1] = {title: "project manager", skills:[skill[0], skill[1]]};

however this doesn't work. How do I make it so a job object array element can create an array from elements of an outside array? (whew that's a mouthful) The main issue seems to be the array of outside array elements since when I take that part out, it starts working again

但这不起作用。如何使作业对象数组元素可以从外部数组的元素创建数组? (这是一个满口)主要问题似乎是外部数组元素的数组,因为当我把那部分拿出来时,它再次开始工作

2 个解决方案

#1


2  

skill doesnt look like it's defined.

技能看起来不像它的定义。

var a_skill = ['problem solving', 'collaboration', 'public speaking'];

var o_job[0] = {title: "developer", skills:[a_skill[0], a_skill[2]]};
var o_job[1] = {title: "project manager", skills:[a_skill[0], a_skill[1]]};

#2


0  

Probably the most appropriate way to do this would be to encapsulate the external array in an object as well, so that you can insert into it using an accessor. An alternative would be to make a static class with helper methods to do this. Also make sure that your arrays are all declared at appropriate access levels (private, public etc.)

可能最合适的方法是将外部数组封装在一个对象中,以便您可以使用访问器插入其中。另一种方法是使用辅助方法创建一个静态类来执行此操作。还要确保所有数组都以适当的访问级别(私有,公共等)声明。

#1


2  

skill doesnt look like it's defined.

技能看起来不像它的定义。

var a_skill = ['problem solving', 'collaboration', 'public speaking'];

var o_job[0] = {title: "developer", skills:[a_skill[0], a_skill[2]]};
var o_job[1] = {title: "project manager", skills:[a_skill[0], a_skill[1]]};

#2


0  

Probably the most appropriate way to do this would be to encapsulate the external array in an object as well, so that you can insert into it using an accessor. An alternative would be to make a static class with helper methods to do this. Also make sure that your arrays are all declared at appropriate access levels (private, public etc.)

可能最合适的方法是将外部数组封装在一个对象中,以便您可以使用访问器插入其中。另一种方法是使用辅助方法创建一个静态类来执行此操作。还要确保所有数组都以适当的访问级别(私有,公共等)声明。