如何获取对象,将其设置为特定属性,该属性来自与另一个对象的属性匹配的预定值?

时间:2023-01-01 11:43:45

*** I revised the above question so that it would make more sense based upon the intention of my question and the confusion / missunderstanding one might occur from this code pattern.

***我修改了上面的问题,以便根据我的问题的意图和从这个代码模式可能出现的混淆/误解更有意义。

What I am trying to accomplish in overall terms is the ability to create an object, from being empty, and set it to something that has a property name and at the same time is setting that property name to a value of another object.

我想要在整体上完成的是能够创建一个对象,从空,并将其设置为具有属性名称的东西,同时将该属性名称设置为另一个对象的值。

What this will allow me to do is create an object that will have a property that will contain an object that has propert(ies) which one of them will be the key, value of which that value will be the name of the property in which the new object is set to.

这将允许我做的是创建一个对象,该对象将包含一个对象,该对象具有属性(其中一个将成为键的属性),该值的值将是其中的属性的名称新对象设置为。

Why would I need this or want this new object...

为什么我需要这个或想要这个新对象......

The purpose of this pattern, unto which i see the use for is to obtain an object that is set to an interface/name in which the object that is set to the containing property is of the same name to one of the values in the referenced property's value. It sounds confusing so i created a jsbin to illustrate a solution to exactly what I am asking for... If someone else has a better explanation or title for what my question is please advise.

这个模式的目的,我看到的用途是获取一个设置为接口/名称的对象,其中设置为包含属性的对象与引用的值之一具有相同的名称财产的价值。这听起来很混乱,所以我创建了一个jsbin来说明我正在要求的解决方案......如果其他人对我的问题有更好的解释或标题,请指教。

The answer I provide along with code sample is what I am asking and achieving through my understanding.

我提供的答案和代码示例是我通过我的理解要求和实现的。

------PREVIOUS QUESTION--------

The question i have is why or when should one decide to take an objects property and set it to another object. For me, it seems like inheritence "setting" is the reason but I am not 100% sure. Furthermore, I am not sure when i should think of this and in what situations I should decide to use such a pattern.

我的问题是为什么或何时应该决定采用对象属性并将其设置为另一个对象。对我来说,似乎继承“设置”是原因,但我不是100%肯定。此外,我不确定何时应该考虑这个以及在什么情况下我应该决定使用这种模式。

I can read the code and follow it but the most confusion I have and question relating to is specifically the object[property] = otherObject;

我可以阅读代码并遵循它,但我遇到的最混乱和问题是具体的对象[property] = otherObject;

Perhaps this has to do with the Object.assign() construction? What I do know is I have seen this done a few times and a little confused to as why and when i should know to use such a pattern. Still, there may be something else I am missing here.

也许这与Object.assign()构造有关?我所知道的是,我已经看过几次,有点困惑,为什么以及何时应该知道使用这种模式。不过,这里可能还有其他一些我不想要的东西。

Here is the code example I am referencing.

这是我引用的代码示例。

constructor(props) {
super(props);
this.state = {
  completedAssets : 0,
  totalAssets : 0,
  currentDownloads: {},
  logs: []
};
}

let currDownloadObject = {
  fileName: fileName,
  currentSize: loaded,
  totalSize: total,
  complete: (loaded === total),
  completeOrder : fileCompleteOrder,
  order: (file) ? file.order : Object.keys(this.state.currentDownloads).length + 1
}

let currentDownloadStateArray = Object.assign({},this.state.currentDownloads);

currentDownloadStateArray[fileName] = currDownloadObject;

this.setState({currentDownloads: currentDownloadStateArray});

Here is an example of the output that is the most confusing for me.

这是一个对我来说最容易混淆的输出示例。

when running this log statement:

运行此日志语句时:

console.log('currentDownloadStateArray ', currentDownloadStateArray);

I get this example of a return:

我得到一个返回的例子:

https://navigation/az.png: {fileName: "https://navigation/az.png", currentSize: 7451, totalSize: 7451, complete: true, completeOrder: 1, …}
https://navigation/destination.png: {fileName: "https://avigation/destination.png", currentSize: 8322, totalSize: 8322, complete: true, completeOrder: 2, …}

So now, in effect, currentDownloadStateArray is set to an object with an interface of each of it's fileName's property. <<< Why is this happening and what is it used for

所以现在,实际上,currentDownloadStateArray被设置为一个对象,该对象具有每个fileName属性的接口。 << <为什么会发生这种情况以及它用于什么< p>

1 个解决方案

#1


0  

To create a new object that is set to a value by another object which contains that object you would do the following.

要创建由包含该对象的另一个对象设置为值的新对象,请执行以下操作。

let state = {
  iteratedFoo: {} // this is a dictionary of dictionaried objects... an object which is a collection of objects.
};

let currentDownloadState = (someValue, anotherValue, urlName) => {

let bar = {
  fileName: urlName,
  someProp: someValue,
  anotherProp: anotherValue
}

let foo = Object.assign({}, state['iteratedFoo'] )

// console.log('foo[urlName] = ', foo[urlName]); // undefined because it wasn't set yet on first loop

foo[urlName] = bar;

console.log('bar ', bar);
console.log('foo ', foo);
console.log('foo[urlName] = ', foo[urlName]);

state['iteratedFoo'] = foo;

console.log('state["iteratedFoo"] ', state['iteratedFoo']);
}

currentDownloadState('this value', 'another value', 'www.msn.com');
currentDownloadState('this value', 'another value', 'www.espn.com');
currentDownloadState('this value', 'another value', 'www.theverge.com');

As this function is repeated the iteratedFoo object creates an object that is a collection of the foo object which contains a property name that contains a value that has properties where one of them is the value of the foo property name... in this case foo[urlName]

当重复此函数时,iteratedFoo对象创建一个对象,该对象是foo对象的集合,其中包含一个属性名称,该属性名称包含一个属性,其中一个属性是foo属性名称的值...在这种情况下为foo [URLNAME]

http://jsbin.com/linebap/1/edit?js,console

The purpose and usecase for this would ultimately to create a collection of objects that are referneced by a property value of said referenced object.

用于此的目的和用例最终将创建由所述引用对象的属性值引用的对象集合。

the resulting state.iteratedFoo object now looks like this...

生成的state.iteratedFoo对象现在看起来像这样......

"state[\"iteratedFoo\"] "
[object Object] {
 www.espn.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.espn.com",
    someProp: "this value"
  },
  www.msn.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.msn.com",
    someProp: "this value"
  },
 www.theverge.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.theverge.com",
    someProp: "this value"
  }
}

#1


0  

To create a new object that is set to a value by another object which contains that object you would do the following.

要创建由包含该对象的另一个对象设置为值的新对象,请执行以下操作。

let state = {
  iteratedFoo: {} // this is a dictionary of dictionaried objects... an object which is a collection of objects.
};

let currentDownloadState = (someValue, anotherValue, urlName) => {

let bar = {
  fileName: urlName,
  someProp: someValue,
  anotherProp: anotherValue
}

let foo = Object.assign({}, state['iteratedFoo'] )

// console.log('foo[urlName] = ', foo[urlName]); // undefined because it wasn't set yet on first loop

foo[urlName] = bar;

console.log('bar ', bar);
console.log('foo ', foo);
console.log('foo[urlName] = ', foo[urlName]);

state['iteratedFoo'] = foo;

console.log('state["iteratedFoo"] ', state['iteratedFoo']);
}

currentDownloadState('this value', 'another value', 'www.msn.com');
currentDownloadState('this value', 'another value', 'www.espn.com');
currentDownloadState('this value', 'another value', 'www.theverge.com');

As this function is repeated the iteratedFoo object creates an object that is a collection of the foo object which contains a property name that contains a value that has properties where one of them is the value of the foo property name... in this case foo[urlName]

当重复此函数时,iteratedFoo对象创建一个对象,该对象是foo对象的集合,其中包含一个属性名称,该属性名称包含一个属性,其中一个属性是foo属性名称的值...在这种情况下为foo [URLNAME]

http://jsbin.com/linebap/1/edit?js,console

The purpose and usecase for this would ultimately to create a collection of objects that are referneced by a property value of said referenced object.

用于此的目的和用例最终将创建由所述引用对象的属性值引用的对象集合。

the resulting state.iteratedFoo object now looks like this...

生成的state.iteratedFoo对象现在看起来像这样......

"state[\"iteratedFoo\"] "
[object Object] {
 www.espn.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.espn.com",
    someProp: "this value"
  },
  www.msn.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.msn.com",
    someProp: "this value"
  },
 www.theverge.com: [object Object] {
    anotherProp: "another value",
    fileName: "www.theverge.com",
    someProp: "this value"
  }
}