Javascript基于特定值合并数组中的对象

时间:2018-03-26 04:58:40

标签: javascript reactjs

我有以下代码:

var dictionary = this.props.posts
const postItemsArr = Object.keys(dictionary).map(post=>dictionary[post])

哪个生成这样的东西: Image to show exactly what I get from postItemsArr

这是0(对象),1(数组)的格式,在这种情况下,0将是业务;其中1是一系列工作。

"jobs":[  
      {  
         "jobId":"Ky4j9mV",
         "instabook":0,
         "be_name":"Faun Skyles",
         "min_comp":"asdasdasd",
         "businessID":"1770"
      },
      {  
         "jobId":"Ky4j9mV",
         "instabook":0,
         "be_name":"Faun Skyles",
         "min_comp":"asdasdasd",
         "businessID":"1771"
      }
   ]
  "businesses":{
    "1770":{
      "neighborhood":name
    }
     "1771":{
      "neighborhood":name
    }
  }

我的目标是; 正如你所看到的,postItemsArr给了我多个数组; 我想创建一个新的数组;并比较postItemsArray中的项目。

所以我们有以下:[0],1如图所示。

[1] --> "1770":{
          "neighborhood":"green"
        }
         "1771":{
          "neighborhood":"black"
        }

[0] --> [  
          {  
             "jobId":"Ky4j9mV",
             "instabook":0,
             "be_name":"Faun Skyles",
             "min_comp":"asdasdasd",
             "businessID":"1770"
          },
          {  
             "jobId":"Ky4j9mV",
             "instabook":0,
             "be_name":"Faun Skyles",
             "min_comp":"asdasdasd",
             "businessID":"1771"
          }
       ]

0和1他们有一些共同点,即businessID;现在我想创建一个新数组;并将1,2合并为一个字典。

输出:[business1objectwithId1770,business1objectwithId1771]

所以我们加入了数组中的Jobs,业务数组。

所以在这种情况下,输出将是这样的:

"jobs":[  
      {  
         "jobId":"Ky4j9mV",
         "instabook":0,
         "be_name":"Faun Skyles",
         "min_comp":"asdasdasd",
         "businessID":"1770"
         "neighborhood":"green"

      },
      {  
         "jobId":"Ky4j9mV",
         "instabook":0,
         "be_name":"Faun Skyles",
         "min_comp":"asdasdasd",
         "businessID":"1771"
         "neighborhood":"black"

      }
   ]

请注意我希望邻居从企业对象合并到与其businessID相对应的作业数组。

2 个答案:

答案 0 :(得分:0)

如果要根据某些内容的值返回一个新数组,请使用filter

示例1

const postItemsArr = Object.keys(dictionary).filter(post=>dictionary[post] === 'somevaluetofilter')

此示例将返回所有等于somevaluetofilter的值。

示例2

const filterJobsById = this.state.jobs.filter(item => item.businessID === 1771)

这将返回一个包含业务ID 1771

的对象的数组

将嵌套对象属性与包含等效ID

的数组中的对象合并
import React, { Component } from "react";
import { render } from "react-dom";

class App extends Component {
  state = {
    jobs: [
      {
        jobId: "Ky4j9mV",
        instabook: 0,
        be_name: "Faun Skyles",
        min_comp: "asdasdasd",
        businessID: "1770"
      },
      {
        jobId: "Ky4j9mV",
        instabook: 0,
        be_name: "Faun Skyles",
        min_comp: "asdasdasd",
        businessID: "1771"
      }
    ],
    businesses: {
      "1770": {
        neighborhood: "SF"
      },
      "1771": {
        neighborhood: "los angeles"
      }
    }
  };

  render() {
    const { jobs, businesses } = this.state;
    const allIds = Object.keys(businesses).map(item => item);
    const updatedJobs = jobs.map((item, index) => {
      console.log(businesses[allIds[index]].neighborhood);
      return {
        jobId: item.jobId,
        instabook: item.instabook,
        be_name: item.be_name,
        min_comp: item.min_comp,
        businessID: item.businessID,
        location: allIds.includes(item.businessID)
          ? businesses[item.businessID].neighborhood
          : null
      };
    });

    console.log("updated", updatedJobs);
    console.log(allIds);
    return (
      <div>
        <p> {updatedJobs.map(item => <div> This is the location added {item.location} </div> </p>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

这将以您希望的方式合并数组。很简单,我们检查businessID是否包含在业务嵌套对象数据结构中。如果是,那么我们就知道我们可以使用该ID来访问该位置。

allIds.includes(item.businessID) ? businesses[item.businessID].neighborhood : null

如果该ID存在,则抓住该邻域,如果没有,则我们没有该业务的邻居。因此,该位置将为null

businesses[item.businessID].neighborhood可以改写如下。

  • businesses[1770].neighborhood等于&#34; SF&#34;
  • business[1771].neighborhood等于&#34; LA&#34;

我们通过key访问对象,IDS是keys

Edit merge attribute of nested objects with object within array containing an equivalent id

答案 1 :(得分:0)

我会有点懒,并假设你已经有了过滤列表,现在你只是想合并。您只需find元素并将数据附加到该元素即可。

由于javascript如何处理对象(正在引用),因此更改会继续进行。

let toMerge = { "1770":{
          "neighborhood":"green"
        },
         "1771":{
          "neighborhood":"black"
        }
}

let filtered = [  
          {  
             "jobId":"Ky4j9mV",
             "instabook":0,
             "be_name":"Faun Skyles",
             "min_comp":"asdasdasd",
             "businessID":"1770"
          },
          {  
             "jobId":"Ky4j9mV",
             "instabook":0,
             "be_name":"Faun Skyles",
             "min_comp":"asdasdasd",
             "businessID":"1771"
          }
       ]
       
for (let key in toMerge)
{
  let theElement = filtered.find( element => element.businessID == key)

  for (let innerKey in toMerge[key])
  {
    theElement[innerKey] = toMerge[key][innerKey]
  }
  
}

console.log(filtered)