我从另一个文件{groupData}导入了数据(因此从技术上讲,我认为这是一个包含对象数组的对象)。目前,该数组仅包含5个对象,但长度可变,可能包含更多对象。每个对象看起来像这样:
req.cookies.token
我的目标是从数组中取出每个对象,修改数据,然后将这些对象放入称为“组”的空有状态数组中。我希望每个对象在进入新的“组”数组之前看起来都像这样:
{
name: "A Name",
img: "https://imgURL.goes.here",
details: "This is a fun group about fun things.",
likes: 45,
},
我曾想过破坏数组,但是如果要向数组中添加内容,则此解决方案不可扩展:
{
name: "A Name",
img: "https://imgURL.goes.here",
details: "This is a fun group about fun things.",
hasNotification: Boolean,
userIsAdmin: Boolean,
},
最有效的方法是什么?谢谢!!
答案 0 :(得分:0)
由于缺乏上下文,我不确定您需要什么。但是您可以使用for循环遍历数组中的每个对象。
如果可以审核现有数据,则可以:
public function image()
{
return $this->hasOne('App\Image')->orderBy('id', 'desc');
}
如果您不想更改原始数据:
User::has('image')->with('image')
如果使用类组件,则相应地调整setState部分