请问一下如果吧json data和shop_id 和 admin_id 合并在一个数组呢?

请问一下如果吧json data 和shop_id 和 admin_id 合并在一个数组呢?
laravel php ,react 这两种都可以, 有谁知道吗? 麻烦帮一下 非常感谢

Axiox post

    import React, {useState} from 'react';
    import axios from 'axios';
    
    const handleProductSeletion = (data: any) => {
        axios.post('/api/tracking/', {
             data, //这里的数据是通过data是外部获取到的
             shop_id: 12345,
             admin_id: 123,
             // 我想要的是 data + shop_id + admin_id 插入到数据库
        });
    };

Laravel Controllers Php

public function store(Request $request)
{
    $result = $request->input();
    return ($result);
}

后台接收到的数据数据

{
    "data": [
        {

            "handle": "0-black", 
            "hasOnlyDefaultVariant": true, 
            "id": "6805297037467", 
            "images": [
                {
                    "id": "30926270627995", 
                    "originalSrc": "OfficialWebsite.jpg"
                }
            ], 
            "productType": "", 
            "publishedAt": "2021-06-08T07:31:02Z", 
            "status": "ACTIVE"
        }, 
        {

            "handle": "0-black", 
            "hasOnlyDefaultVariant": true, 
            "id": "6805297037468", 
            "images": [
                {
                    "id": "30926270627995", 
                    "originalSrc": "OfficialWebsite.jpg"
                }
            ], 
            "productType": "", 
            "publishedAt": "2021-06-08T07:31:02Z", 
            "status": "ACTIVE"
        }, 
    ], 
    "shop_id": 12345,  //想合并shop_id
    "admin_id": 123 //想合并admin_id 
}

想要的效果
{

"data": [
    {

        "handle": "0-black", 
        "hasOnlyDefaultVariant": true, 
        "id": "6805297037467", 
        "images": [
            {
                "id": "30926270627995", 
                "originalSrc": "OfficialWebsite.jpg"
            }
        ], 
        "productType": "", 
        "publishedAt": "2021-06-08T07:31:02Z", 
        "status": "ACTIVE",
        "influencers_id": 123
        "shop_id": 12345, 
    }, 
    {

        "handle": "0-black", 
        "hasOnlyDefaultVariant": true, 
        "id": "6805297037468", 
        "images": [
            {
                "id": "30926270627995", 
                "originalSrc": "OfficialWebsite.jpg"
            }
        ], 
        "productType": "", 
        "publishedAt": "2021-06-08T07:31:02Z", 
        "status": "ACTIVE",
        "admin_id": 123,
        "shop_id": 12345, 
    }, 
], 

}

阅读 2.6k
3 个回答
var data = [
  {
    handle: "0-black",
    hasOnlyDefaultVariant: true,
    id: "6805297037467",
    images: [
      {
        id: "30926270627995",
        originalSrc: "OfficialWebsite.jpg"
      }
    ],
    productType: "",
    publishedAt: "2021-06-08T07:31:02Z",
    status: "ACTIVE"
  },
  {
    handle: "0-black",
    hasOnlyDefaultVariant: true,
    id: "6805297037468",
    images: [
      {
        id: "30926270627995",
        originalSrc: "OfficialWebsite.jpg"
      }
    ],
    productType: "",
    publishedAt: "2021-06-08T07:31:02Z",
    status: "ACTIVE"
  }
];
data = data.map((item) =>Object.assign(item, {admin_id: 123, shop_id: 12345 }));

// 发送参数
var params = { data: data };
console.log(params);

// 结果

{
    "data": [
        {
            "handle": "0-black",
            "hasOnlyDefaultVariant": true,
            "id": "6805297037467",
            "images": [
                {
                    "id": "30926270627995",
                    "originalSrc": "OfficialWebsite.jpg"
                }
            ],
            "productType": "",
            "publishedAt": "2021-06-08T07:31:02Z",
            "status": "ACTIVE",
            "admin_id": 123,
            "shop_id": 12345
        },
        {
            "handle": "0-black",
            "hasOnlyDefaultVariant": true,
            "id": "6805297037468",
            "images": [
                {
                    "id": "30926270627995",
                    "originalSrc": "OfficialWebsite.jpg"
                }
            ],
            "productType": "",
            "publishedAt": "2021-06-08T07:31:02Z",
            "status": "ACTIVE",
            "admin_id": 123,
            "shop_id": 12345
        }
    ]
}

https://codepen.io/1567887123...

前端用Array.prototype.map, php用array_map

axios.post('/api/tracking/', {
    data: data.map(item => ({...item, shop_id: 12345, admin_id: 123}))
});
const {data, ...arg} = obj;

obj.data = data.map(item => Object.assign(item, arg));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题