我有一个包含键和数据的对象。我想从钥匙中获得上一个物品。
我以前见过类似的话题,但是解决方案确实很复杂。
我还添加了一个不起作用的功能,但我希望它能如何工作
简化代码
let obj = {
slug: 'my-page',
title: 'My title',
description: 'My description',
content: 'My content'
};
let previous = obj.previousItem('description'); // Does not work
console.log(previous);
预期结果
{标题:“我的标题” }
答案 0 :(得分:-3)
对象以前是没有顺序的,但实际上它们是有顺序的,我不会依赖此顺序。
但是无论如何,您都可以按下一个键/值对进行过滤并获得一个对象。
let obj = { slug: 'my-page', title: 'My title', description: 'My description', content: 'My content' },
previous = Object.fromEntries(Object
.entries(obj)
.filter((_, i, { [i + 1]: next = [] }) => next[0] === 'description')
);
console.log(previous);
答案 1 :(得分:-3)
我认为是这样
Object.prototype.previousItem = function(propName) {
const arr = Object.keys(this);
let indx = arr.indexOf(propName)-1;
if (indx<0) return null;
const result = {};
result[arr[indx]] = this[arr[indx]];
return result;
}
答案 2 :(得分:-3)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cooperado;
class CooperadoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
//$cooperados = Cooperado::all();
$cooperados = Cooperado::orderBy('dtCad', 'desc')->paginate(10);
return view('cooperados.index', compact('cooperados'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('cooperados.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate([
'nameCoop'=>'required',
'numCoop'=> 'required|integer',
'cpfCoop'=> 'required',
'dtCad'=>'required|date',
'subscritas'=>'required'
]);
$cooperado = new Cooperado([
'nameCoop' => $request->get('nameCoop'),
'numCoop'=> $request->get('numCoop'),
'cpfCoop'=> $request->get('cpfCoop'),
'dtCad'=> $request->get('dtCad'),
'description'=> $request->get('description'),
'subscritas'=> $request->get('subscritas'),
'aintegralizar'=> $request->get('subscritas'),
'status'=> $request->get('status')
]);
$cooperado->save();
return redirect('/cooperados')->with('success', 'Cooperado adicionado');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$cooperado = Cooperado::find($id);
return view('cooperados.show', compact('cooperado'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
$cooperado = Cooperado::find($id);
return view('cooperados.edit', compact('cooperado'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
$request->validate([
'nameCoop'=>'required',
'numCoop'=> 'required|integer',
'cpfCoop'=> 'required',
'dtCad'=>'required|date',
'subscritas'=>'required'
]);
$cooperado = Cooperado::find($id);
$cooperado->nameCoop = $request->get('nameCoop');
$cooperado->numCoop = $request->get('numCoop');
$cooperado->cpfCoop = $request->get('cpfCoop');
$cooperado->dtCad = $request->get('dtCad');
$cooperado->dtDem = $request->get('dtDem');
$cooperado->description = $request->get('description');
$cooperado->subscritas = $request->get('subscritas');
$cooperado->integralizadas = $request->get('integralizadas');
$cooperado->aintegralizar = $request->get('aintegralizar');
$cooperado->status = $request->get('status');
$cooperado->save();
return redirect('/cooperados')->with('success', 'Cooperado atualizado');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
$cooperado = Cooperado::find($id);
$cooperado->delete();
return redirect('/cooperados')->with('success', 'Sucess');
}
}
答案 3 :(得分:-3)
您可以尝试按照以下操作获得上一项
let object = {
slug: 'my-page',
title: 'My title',
description: 'My description',
content: 'My content'
}
let previous = previousItem(object, 'description');
console.log(previous);
function previousItem(object, str) {
let lastKey = "";
for (const key in object) {
if (object.hasOwnProperty(key)) {
if (key == str){
lastKey = lastKey || key;
let obj = { };
obj[lastKey] = object[lastKey];
return obj;
} else {
lastKey = key;
}
}
}
}