如何创建一个函数,该函数可以创建具有N个属性的N个对象?

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

How to create a function that creates N(numbers) of objects with a N properties?

问题

我正在尝试在JS和Go中创建一个函数,用于创建具有N个属性的N个对象。

我想要一个函数,返回N个具有N个属性的对象。目前我有以下代码:

function objects(name, age, idioms, school) { 
    this.name = name;
    this.age = age;
    this.idioms = idioms;
    this.school = school;
}

我甚至不知道如何在Go中实现这个功能。

英文:

I'm trying to make a function in JS, also in Go, to make N objects with N properties.

I want a function that returns N objects with N properties. So for I got this:

function objects(name, age, idioms,school) { 
    this.name = name;
    this.age = age;
    this.idioms= idioms;
    this.school = school;
}

I don't even know how to do this in Go.

答案1

得分: 2

这只是一个关于在Go语言中使用maps的简单示例。您还可以使用类似的逻辑,使用具有固定属性的structs。该函数接受一个整数值N和一个字符串数组,这些字符串数组是属性。

package main

import (
	"fmt"
)

func createDynamicMap(n int, pr []string) ([]map[string]interface{}) {
	var listOfMap []map[string]interface{}
	for i := 0; i < n; i++ {
		dm := make(map[string]interface{})
		for _, v := range pr {
			if _, ok := dm[v]; !ok {
				dm[v] = nil // 所有属性都初始化为nil
			}
		}
		listOfMap = append(listOfMap, dm)
	}
	return listOfMap
}

func main() {
	dynamicMap := createDynamicMap(10,[]string{"name","age","gender"})
	fmt.Println(len(dynamicMap))
}

以上是代码示例。

英文:

This is just an simple example with maps in golang. You can also use a similar logic with using structs with fixed props. The function takes an int value N, and a string array which are the props.

package main

import (
	&quot;fmt&quot;
)

func createDynamicMap(n int, pr []string) ([]map[string]interface{}) {
	var listOfMap []map[string]interface{}
	for i := 0; i &lt; n; i++ {
		dm := make(map[string]interface{})
		for _, v := range pr {
			if _, ok := dm[v]; !ok {
				dm[v] = nil // all props initialised as nil
			}
		}
		listOfMap = append(listOfMap, dm)
	}
	return listOfMap
}

func main() {
	dynamicMap := createDynamicMap(10,[]string{&quot;name&quot;,&quot;age&quot;,&quot;gender&quot;})
	fmt.Println(len(dynamicMap))
}

答案2

得分: 1

我无法回答关于Golang部分的问题,但对于JavaScript部分,最好是使用类(class)创建新的对象实例。首先创建一个类,然后通过循环遍历对象的属性(可以使用Object.entries方法),并在新的类实例中进行实例化。

以下是示例代码:

class Creator {
  // args可以是一个具有任意数量属性的对象
  constructor(args) {
    // 只需遍历属性并将每个值分配给实例
    Object.entries(args).forEach(([key, value]) => {
      this[key] = value;
    });
  };
}

const obj = { name: 'Bob', age: 2, idioms: 'etc', school: 'Grange Hill' };
const obj2 = { name: 'Steve', job: 'Farmer' };

console.log(new Creator(obj));
console.log(new Creator(obj2));

希望对你有帮助!

英文:

I can't answer the Golang part, but for the JavaScript you'd be better off making new object instances from a class. Create the class, and then pass in objects the properties of which you can loop over and instantiate in the new class instance.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class Creator { 

  // args can be an object with n amount of
  // properties
  constructor(args) {

    // Just loop over the entries and assign each value
    // to the instance
    Object.entries(args).forEach(([key, value]) =&gt; {
      this[key] = value;
    });
  };
}

const obj = { name: &#39;Bob&#39;, age: 2, idioms: &#39;etc&#39;, school: &#39;Grange Hill&#39; };
const obj2 = { name: &#39;Steve&#39;, job: &#39;Farmer&#39; };

console.log(new Creator(obj));
console.log(new Creator(obj2));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2021年8月1日 02:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/68604907.html
匿名

发表评论

匿名网友

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

确定