Javascript - 过滤对象中的数组

时间:2018-04-06 05:12:10

标签: javascript

我正在尝试使用过滤值创建一个对象数组

// desired output

     [
         { "2017-02-01": "09:00" },
         { "2017-02-02": "09:00" },
     ]

我的Js对象

let res = {
  "2018-02-01": [
    {
      "time": "09:00",
      "available": true,
    },
    {
      "time": "10:00",
      "available": false,
    }
  ],
  "2018-02-02": [
    {
      "time": "09:00",
      "available": true,
      "reference": null
    },
    {
      "time": "10:00",
      "available": false,
      "reference": null
    }
  ]
}

// My attempt 
output = _.keys(res).map( i => res[i].filter( t => t.available))

console.log(output); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>

4 个答案:

答案 0 :(得分:3)

试试这个:

let res = {
  "2018-02-01": [
    {
      time: "09:00",
      available: true
    },
    {
      time: "10:00",
      available: false
    }
  ],
  "2018-02-02": [
    {
      time: "09:00",
      available: true,
      reference: null
    },
    {
      time: "10:00",
      available: false,
      reference: null
    }
  ]
};
let output = Reflect.ownKeys(res).map(key => ({
  [key]: res[key].filter(obj => obj.available)[0].time
}));
console.log(output);

您的逻辑基本上是正确的,因为您没有很好地控制return value map函数中的[key]: /*basically your code*/[0].time

这就是我所做的:

Observable

答案 1 :(得分:1)

如果我理解正确,你需要的是第一个可用的时间段。

为此,您可以遍历响应键。发布您可以使用_.findlet res = { "2018-02-01": [{ "time": "09:00", "available": true, }, { "time": "10:00", "available": false, } ], "2018-02-02": [{ "time": "09:00", "available": true, "reference": null }, { "time": "10:00", "available": false, "reference": null } ] } // My attempt output = Object.keys(res).reduce(function(acc, key) { const obj = res[key].find(x => !!x.available); if (!!obj) acc.push({ [key]: obj.time }); return acc; }, []) console.log(output);并获取可用的第一个值。

现在创建一个对象,其日期值为关键,值为object.time

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
    `root
     |-- partnumber: string (nullable = true)
     |-- brandlabel: string (nullable = true)
     |-- availabledate: string (nullable = true)
     |-- description: array (nullable = true)
     |    |-- element: struct (containsNull = true)
     |    |    |-- #VALUE: string (nullable = true)
     |    |    |-- @DescriptionCode: string (nullable = true)
     |    |    |-- @LanguageCode: string (nullable = true)
     |    |    |-- @Language: string (nullable = true)`

    suppose We want to concatenate 2 struct fields as one string separated by :,next 2 struct fields as another column.


root
 |-- partnumber: string (nullable = true)
 |-- brandlabel: string (nullable = true)
 |-- availabledate: string (nullable = true)
 |-- descriptions: array (nullable = true)
 |-- |--   element1: string (containsNull = true)
 |-- |--   element2: string (containsNull = true)

答案 2 :(得分:1)

使用available循环遍历true对象。 使用Array.find方法查找key/valuepush的对象。 将let res = { "2018-02-01": [ { "time": "09:00", "available": true, }, { "time": "10:00", "available": false, } ], "2018-02-02": [ { "time": "09:00", "available": true, "reference": null }, { "time": "10:00", "available": false, "reference": null } ] } var arr = []; for (var date in res) { arr.push({ [date]: res[date].find(r => r.available).time }); } console.log(arr);存储在对象中,并将04-06 10:48:41.785 9224-9353/test.ms.authentication E/HttpWebRequest: SERVER_ERROR:2018-04-06 05:18:41-fe96c34f-dd35-4d07-98ee-cdff28eab452-IOException:unexpected end of stream on Connection{login.windows.net:443, proxy=DIRECT hostAddress=40.112.64.25 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1} (recycle count=0) ver:1.13.1 04-06 10:48:41.791 9224-9353/test.ms.authentication E/Oauth: SERVER_ERROR:2018-04-06 05:18:41-fe96c34f-dd35-4d07-98ee-cdff28eab452-unexpected end of stream on Connection{login.windows.net:443, proxy=DIRECT hostAddress=40.112.64.25 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1} (recycle count=0) ver:1.13.1 java.io.IOException: unexpected end of stream on Connection{login.windows.net:443, proxy=DIRECT hostAddress=40.112.64.25 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1} (recycle count=0) at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210) at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80) at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java) at com.microsoft.aad.adal.HttpWebRequest.send(HttpWebRequest.java:130) at com.microsoft.aad.adal.WebRequestHandler.sendPost(WebRequestHandler.java:76) at com.microsoft.aad.adal.Oauth2.postMessage(Oauth2.java:455) at com.microsoft.aad.adal.Oauth2.getTokenForCode(Oauth2.java:435) at com.microsoft.aad.adal.Oauth2.getToken(Oauth2.java:396) at com.microsoft.aad.adal.AcquireTokenInteractiveRequest.acquireTokenWithAuthCode(AcquireTokenInteractiveRequest.java:95) at com.microsoft.aad.adal.AcquireTokenRequest$3.run(AcquireTokenRequest.java:675) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.io.EOFException: \n not found: size=0 content=... at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200) at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191) at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)  at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)  at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)  at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)  at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)  at com.microsoft.aad.adal.HttpWebRequest.send(HttpWebRequest.java:130)  at com.microsoft.aad.adal.WebRequestHandler.sendPost(WebRequestHandler.java:76)  at com.microsoft.aad.adal.Oauth2.postMessage(Oauth2.java:455)  at com.microsoft.aad.adal.Oauth2.getTokenForCode(Oauth2.java:435)  at com.microsoft.aad.adal.Oauth2.getToken(Oauth2.java:396)  at com.microsoft.aad.adal.AcquireTokenInteractiveRequest.acquireTokenWithAuthCode(AcquireTokenInteractiveRequest.java:95)  at com.microsoft.aad.adal.AcquireTokenRequest$3.run(AcquireTokenRequest.java:675)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)  at java.lang.Thread.run(Thread.java:761)  04-06 10:48:41.798 9224-9353/test.ms.authentication E/AcquireTokenRequest: AUTHORIZATION_CODE_NOT_EXCHANGED_FOR_TOKEN:2018-04-06 05:18:41-fe96c34f-dd35-4d07-98ee-cdff28eab452-Error in processing code to get token. Request authority:https://login.windows.net/abccompanynet.onmicrosoft.com resource:https://graph.windows.net clientid:7feaade0-79a7-4c96-b643-c78c41cf4ec3 CorrelationId: fe96c34f-dd35-4d07-98ee-cdff28eab452unexpected end of stream on Connection{login.windows.net:443, proxy=DIRECT hostAddress=40.112.64.25 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1} (recycle count=0) ver:1.13.1 Error in processing code to get token. Request authority:https://login.windows.net/abccompanynet.onmicrosoft.com resource:https://graph.windows.net clientid:7feaade0-79a7-4c96-b643-c78c41cf4ec3 CorrelationId: fe96c34f-dd35-4d07-98ee-cdff28eab452 com.microsoft.aad.adal.AuthenticationException: Error in processing code to get token. Request authority:https://login.windows.net/abccompanynet.onmicrosoft.com resource:https://graph.windows.net clientid:7feaade0-79a7-4c96-b643-c78c41cf4ec3 CorrelationId: fe96c34f-dd35-4d07-98ee-cdff28eab452 at com.microsoft.aad.adal.AcquireTokenInteractiveRequest.acquireTokenWithAuthCode(AcquireTokenInteractiveRequest.java:101) at com.microsoft.aad.adal.AcquireTokenRequest$3.run(AcquireTokenRequest.java:675) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.io.IOException: unexpected end of stream on Connection{login.windows.net:443, proxy=DIRECT hostAddress=40.112.64.25 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1} (recycle count=0) at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210) at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80) at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java) at com.microsoft.aad.adal.HttpWebRequest.send(HttpWebRequest.java:130) at com.microsoft.aad.adal.WebRequestHandler.sendPost(WebRequestHandler.java:76) at com.microsoft.aad.adal.Oauth2.postMessage(Oauth2.java:455) at com.microsoft.aad.adal.Oauth2.getTokenForCode(Oauth2.java:435) at com.microsoft.aad.adal.Oauth2.getToken(Oauth2.java:396) at com.microsoft.aad.adal.AcquireTokenInteractiveRequest.acquireTokenWithAuthCode(AcquireTokenInteractiveRequest.java:95) at com.microsoft.aad.adal.AcquireTokenRequest$3.run(AcquireTokenRequest.java:675)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)  at java.lang.Thread.run(Thread.java:761)  Caused by: java.io.EOFException: \n not found: size=0 content=... at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200) at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191) at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)  at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)  at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)  at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)  at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)  at com.microsoft.aad.adal.HttpWebRequest.send(HttpWebRequest.java:130)  at com.microsoft.aad.adal.WebRequestHandler.sendPost(WebRequestHandler.java:76)  at com.microsoft.aad.adal.Oauth2.postMessage(Oauth2.java:455)  at com.microsoft.aad.adal.Oauth2.getTokenForCode(Oauth2.java:435)  at com.microsoft.aad.adal.Oauth2.getToken(Oauth2.java:396)  at com.microsoft.aad.adal.AcquireTokenInteractiveRequest.acquireTokenWithAuthCode(AcquireTokenInteractiveRequest.java:95)  at com.microsoft.aad.adal.AcquireTokenRequest$3.run(AcquireTokenRequest.java:675)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)  at java.lang.Thread.run(Thread.java:761)  04-06 10:48:41.799 9224-9353/test.ms.authentication V/AcquireTokenRequest: 2018-04-06 05:18:41-fe96c34f-dd35-4d07-98ee-cdff28eab452-Sending error to callback CorrelationId: fe96c34f-dd35-4d07-98ee-cdff28eab452 ver:1.13.1 04-06 10:48:41.801 9224-9353/test.ms.authentication V/AuthenticationContext: 2018-04-06 05:18:41-fe96c34f-dd35-4d07-98ee-cdff28eab452-Remove waiting request: 105490614 ver:1.13.1 04-06 10:48:45.701 9224-9494/test.ms.authentication V/FA: Inactivity, disconnecting from the service 存储在数组中。

public class AmmoManager: MonoBehaviour 
{

    public int ammoCount;
    public Text ammoCountDisplay; // bind this to a Text object in the inspector

    // Use this for initialization
    void Start () 
    {
        ammoCount = 0;
    }

    // Update is called once per frame
    void Update () 
    {
       ammoCountDisplay.text = ammoCount.ToString();
    }

    void CatchFish(int number)
    {
        ammoCount += number;
    }

    void ShotFired(int number)
    {
        ammoCount -= number;
    }
}

答案 3 :(得分:0)

如果您想使用原生javascript,那么您可以使用此代码 -

var arr = [];
for (const [key, value] of Object.entries(res)) {
    var obj = {};

    for (const o of value) {

        if (o.available) { obj[key] = o.time; }
    } 

    arr.push(obj);
}

console.log(arr);